开发者

Open IE and send JSON POST data

I'm trying using PowerShell to open a web page while sending JSON as my POST data.

Here is what I have

$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")

The web page does open however the JSON isn't getting posted according to Fiddler.

How can I send the POST data?


I would use the cURL utility http://curl.haxx.se/dlwiz/?type=bin&os=Win64 and run it from PowerShell. No need to use IE or any browser for that matter.


I've been looking at something similar and I've posted this http://amonkeysden.tumblr.com/post/5842982257/posting-to-a-restful-api-with-powershell - there's a link to the source there as well but more specifically this is the module:

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

and you probably want the New-RestItem function though the Write-URL might do it for you as well.

You should pass the JSON as a string - the example below isn't tested... :).

eg.

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

#>
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜