简体   繁体   English

从Windows Powershell发送HTTP请求的最佳方法是什么?

[英]What is the best way to send HTTP requests from Windows Powershell?

从Windows Powershell发送HTTP请求的最佳方法是什么?

Found one way: 找到一种方法:

$page = (New-Object System.Net.WebClient).DownloadString("http://localhost/")

Thanks to Steven Murawski for his comment: 感谢Steven Murawski的评论:

The best way really depends on what task you are trying to accomplish as the two answers below have noted. 最好的方法实际上取决于你要完成的任务,正如下面的两个答案所指出的那样。 WebClient is the simplest, but HttpWebRequest is the most flexible. WebClient是最简单的,但HttpWebRequest是最灵活的。

在PowerShell 3.0+中,您可以使用Invoke-WebRequest

$page = Invoke-WebRequest "http://localhost/"

System.Net.WebClient is the easiest way to do it for simple GET request. 对于简单的GET请求,System.Net.WebClient是最简单的方法。 However if you need to do a POST request for a form then you will need to use System.Net.HttpWebRequest. 但是,如果您需要对表单执行POST请求,则需要使用System.Net.HttpWebRequest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM