【问题标题】:http requests with powershell使用 powershell 的 http 请求
【发布时间】:2011-12-04 16:00:52
【问题描述】:

我希望使用 powershell 向网页发出 http 请求,这可能吗?如果可以,我该如何实现?

我可以向 https 页面发出请求吗?我可以使用 bat 文件而不是 https 发出 http 请求,希望我可以使用 powershell 进行 https 页面请求。

【问题讨论】:

  • 您是否从具有知名证书颁发机构签署的证书的站点请求内容?浏览器和大多数 HTTP 堆栈都不愿从具有不正确、过期或自签名(测试)证书的 HTTPS 站点检索内容。不过,您通常可以设置一个策略来忽略证书问题或导入有问题的证书。

标签: powershell powershell-2.0


【解决方案1】:

您可以使用 .NET 框架提供的常用 WebRequestHttpWebRequest 类。

$request = [System.Net.WebRequest]::Create('http://example.com')
# do something with $request

这与使用 C# 中的相同类和 API 没有什么不同,除了与 PowerShell 的语法差异。

PowerShell v3 还带来了Invoke-WebRequest 和其他一些。

【讨论】:

  • 是否可以通过上述方式向https页面发出请求?
  • 以防万一有人需要发送 POST 请求,因为这是我需要的下一件事: $request = [System.Net.WebRequest]::Create('mypageurl'); $request.Method = "POST"; $request.ContentType = "应用程序/x-www-form-urlencoded"; $bytes = [System.Text.Encoding]::ASCII.GetBytes("name=john&number=5"); $request.ContentLength = $bytes.Length; $requestStream = $request.GetRequestStream(); $requestStream.Write($bytes, 0, $bytes.Length); $requestStream.Close(); $request.GetResponse();
【解决方案2】:

试试这个:

(New-Object System.Net.WebClient).DownloadString("http://stackoverflow.com")

WebClient.DownloadString Method (String)

或在 PowerShell 3.0 中,

(Invoke-WebRequest http://stackoverflow.com).content

Invoke-WebRequest

【讨论】:

  • 短小精悍,也适用于附加到地址的查询字符串。
  • PowerShell 4 > wgetInvoke-WebRequest“sim-linked”
【解决方案3】:

根据你在做什么,你也可以使用System.Net.WebClient,这是HttpWebRequest的简化抽象

$client = new-object system.net.webclient

在这里寻找不同之处:What difference is there between WebClient and HTTPWebRequest classes in .NET?

PS:使用 Powershell v3.0,您有 Invoke-WebRequestInvoke-RestMethod cmdlet 可用于类似目的

【讨论】:

    【解决方案4】:

    如果一切都失败了,请使用来自 http://curl.haxx.se 的 Curl。您可以设置所有内容,包括证书处理、POST 等。不是很微妙,但它可以工作并处理所有奇怪的情况;例如您可以设置 --insecure 标志来忽略证书名称问题、过期或测试状态。

    【讨论】:

      【解决方案5】:

      您可以使用 Invoke-WebRequest cmdlet 创建 HTTP、HTTPS、FTP 和 FILE 请求。这很容易,并且提供了许多可供选择的选项。 示例:向 google.com

      发出简单的 http/https 请求
      Invoke-WebRequest -Uri "http://google.com"
      

      更多参考可以找到MSDN

      【讨论】:

        【解决方案6】:

        此代码可在 powershell 中通过 https 处理 ASCII 和二进制文件:

        # Add the necessary .NET assembly
        Add-Type -AssemblyName System.Net.Http
        
        # Create the HttpClient object
        $client = New-Object -TypeName System.Net.Http.Httpclient
        
        # Get the web content.
        $task = $client.GetByteArrayAsync("https://stackoverflow.com/questions/7715695/http-requests-with-powershell")
        
        # Wait for the async call to finish
        $task.wait();
        
        # Write to file
        [io.file]::WriteAllBytes('test.html',$task.result)
        

        在 Powershell 5.1.17134.1、Win 10 上测试

        【讨论】:

          【解决方案7】:

          试试这个 PowerShell 模块:https://github.com/toolkitx/simple-request

          Install-Module -Name SimpleRequest安装 然后你可以发送类似的请求

          $Data = @{
              "TokenUrl"     = 111
              "ClientSecret" = "222"
              "ClientId"     = "333"
              "AuthResource" = "444"
              "Username"     = "User1"
              "Password"     = "Password"
              "Id"           = 99
              "Price"        = 0.99
              "Value"        = "Content"
          }
          
          $Sample = '
          POST https://httpbin.org/post?id={{Id}}
          
          Content-Type: application/json
          Authorization: Bearer {{QIBToken}}
          
          {
              "id": {{Id}},
              "value": "{{Value}}"
          }'
          
          $Response = Invoke-SimpleRequest -Syntax $Sample -Context $Data
          

          详细介绍请参考GitHub

          【讨论】:

            【解决方案8】:

            此方法下载内容:

            # PowerShell 2 version
            $WebRequest=New-Object System.Net.WebClient
            $WebRequest.UseDefaultCredentials=$true
            #$WebRequest.Credentials=(Get-Credential)
            $Data=$WebRequest.DownloadData("http://<url>")
            [System.IO.File]::WriteAllBytes("<full path of file>",$Data)
            
            # PowerShell 5 version
            Invoke-WebRequest -Uri "http://<url>" -OutFile "<full path of file>" -UseDefaultCredentials -ContentType 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-08-30
              • 2016-08-06
              • 2020-08-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多