【问题标题】:Open IE and send JSON POST data打开 IE 并发送 JSON POST 数据
【发布时间】:2011-09-25 09:42:54
【问题描述】:

我正在尝试使用 PowerShell 打开网页,同时将 JSON 作为我的 POST 数据发送。

这就是我所拥有的

$ie = New-Object -comObject InternetExplorer.Application
$ie.visible = $true
$postBytes = [System.Text.Encoding]::Default.GetBytes('"xCoord":"11.11","yCoord":"22.22","scale":"8,000,016.00","coordSysId":"123"');
$ie.navigate("http://localhost/ProcessSearch", "_blank", $postBytes, "Content-Type: application/json; charset=utf-8")

网页确实打开了,但是根据 Fiddler 没有发布 JSON。

如何发送 POST 数据?

【问题讨论】:

    标签: json powershell


    【解决方案1】:

    我会使用 cURL 实用程序 http://curl.haxx.se/dlwiz/?type=bin&os=Win64 并从 PowerShell 运行它。无需使用 IE 或任何浏览器。

    【讨论】:

    • 这看起来像一个漂亮的库。我会在未来的项目中寻找它。谢谢。
    【解决方案2】:

    我一直在寻找类似的东西,我已经发布了这个http://amonkeysden.tumblr.com/post/5842982257/posting-to-a-restful-api-with-powershell - 那里也有一个源链接,但更具体地说,这是模块:

    https://bitbucket.org/thompsonson/powershellmodulerepository/src/5e0afe9d0e26/PsUrl/PsUrl.psm1

    您可能需要New-RestItem 功能,尽管Write-URL 也可能为您提供。

    您应该将 JSON 作为字符串传递 - 下面的示例未经过测试... :)。

    例如。

    New-RestItem @987654323@ -Data @{"JSON" = '{"something": "value", "more": {"morekey1":"value", "morekey2": "value"} }'

    function New-RestItem {
    [CmdletBinding()]
    Param(
        [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]    
        [String]$Url,
        [HashTable]$Data,
        [TimeSpan]$Timeout = [System.TimeSpan]::FromMinutes(1)
    )    
        Add-Type -AssemblyName System.Web
        $formData = [System.Web.HttpUtility]::ParseQueryString("")  
        foreach($key in $Data.Keys){
            $formData.Add($key, $Data[$key])        
        }
        Write-Verbose $formData.ToString()
        $reqBody = [System.Text.Encoding]::Default.GetBytes($formData.ToString())   
    
        try{
            $req = [System.Net.WebRequest]::Create($Url)
            $req.Method = "POST"
            $req.ContentType = "application/x-www-form-urlencoded"      
            $req.Timeout = $Timeout.TotalMilliseconds
            $reqStream = $req.GetRequestStream()        
            $reqStream.Write($reqBody, 0, $reqBody.Length)
            $reqStream.Close()
    
            $resp = $req.GetResponse()
            Write-Verbose $resp
            Write-Output $resp.StatusCode
        }
        catch [System.Net.WebException]{
            if ($_.Exception -ne $null -and $_.Exception.Response -ne $null) {
                $errorResult = $_.Exception.Response.GetResponseStream()
                $errorText = (New-Object System.IO.StreamReader($errorResult)).ReadToEnd()
                Write-Warning "The remote server response: $errorText"
                Write-Output $_.Exception.Response.StatusCode
            } else {
                throw $_
            }
        }   
    
    <#
    .Synopsis
        POSTs the data to the given URL
    .Description     
        POSTs the data to the given URL and returns the status code
    .Parameter Url
        URL to POST
    .Parameter Data
        Hashtable of the data to post.
    .Parameter Timeout
        Optional timeout value, by default timeout is 1 minute.
    .Input
        URL and a hash of the data to create
    .Output
        The HTTP Status code (Created (201), Forbidden (403) or Bad Request (400))
    .Example
        PS:> New-RestItem http://www.tumblr.com/api/write -Data @{"Foo" = "Bar" }
    
        Description
        -----------
        POST's data to the tumblr write API as application/x-www-form-urlencoded
    
    #>
    }
    

    【讨论】:

    • 圣母!这看起来很棒。一旦我有时间,我会测试一下。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 2020-01-11
    相关资源
    最近更新 更多