【问题标题】:Are there any HTTP libraries in Powershell that is similar to Requests in Python or Mechanize in Ruby?Powershell 中是否有类似于 Python 中的 Requests 或 Ruby 中的 Mechanize 的 HTTP 库?
【发布时间】:2013-09-04 20:06:30
【问题描述】:

Powershell 中是否有类似于 Python 中的 Requests 或 Ruby 中的 Mechanize 的 HTTP 库? 我想要的是一个 PowerShell 库,它可以轻松发送GETPOST 请求并轻松管理请求之间的 cookie。这种图书馆存在吗?谢谢。

【问题讨论】:

    标签: http powershell http-headers httprequest


    【解决方案1】:

    我不知道你提到的任何一个库,但这听起来像是 Invoke-WebRequest 可以做的事情。为此,您将需要 PowerShell v3。以下是此命令的documentation 的示例。 Herehere 是其他一些示例。

    PS C:\> # Sends a sign-in request by running the Invoke-WebRequest cmdlet. The command specifies a value of "fb" for the SessionVariable parameter, and saves the results in the $r variable.
    
    $r=Invoke-WebRequest http://www.facebook.com/login.php -SessionVariable fb
    
    # Use the session variable that you created in Example 1. Output displays values for Headers, Cookies, Credentials, etc. 
    
    $fb
    
    # Gets the first form in the Forms property of the HTTP response object in the $r variable, and saves it in the $form variable. 
    
    $form = $r.Forms[0]
    
    # Pipes the form properties that are stored in the $forms variable into the Format-List cmdlet, to display those properties in a list. 
    
    $form | Format-List
    
    # Displays the keys and values in the hash table (dictionary) object in the Fields property of the form.
    
    $form.fields
    
    # The next two commands populate the values of the "email" and "pass" keys of the hash table in the Fields property of the form. Of course, you can replace the email and password with values that you want to use. 
    
    $form.Fields["email"] = "User01@Fabrikam.com"
    $form.Fields["pass"] = "P@ssw0rd"
    
    # The final command uses the Invoke-WebRequest cmdlet to sign in to the Facebook web service. 
    
    $r=Invoke-WebRequest -Uri ("https://www.facebook.com" + $form.Action) -WebSession $fb -Method POST -Body $form.Fields
    

    【讨论】:

      【解决方案2】:

      您似乎在寻找 Invoke-WebRequest(需要 PowerShell v3)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 2021-12-29
        • 2016-03-29
        相关资源
        最近更新 更多