打开IE并发送JSON POST数据

时间:2011-06-29 18:20:00

标签: json powershell

我正在尝试使用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数据?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我一直在寻找类似的东西,我发布了这个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 http://www.tumblr.com/api/write -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

#>
}
相关问题