powershell网页getpost 中文请求与返回
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell网页getpost 中文请求与返回相关的知识,希望对你有一定的参考价值。
网上经常看到powershell 传输中文参数,接收中文返回问题,近几天正好遇到了,给大家分享一下。
不需要传中文,powershell原生命令样例:
POST:
$url="" #自行填写网页请求地址
$body=@
"user" = $user
"password"=$password
| ConvertTo-Json #具体参照接口参数修改
Invoke-WebRequest -Uri $url -Method Post -Headers @ "Content-Type" = "application/json" -Body $body
GET:
$url="" #自行填写网页请求地址
Invoke-WebRequest -Uri $url -Method get
需要传中文参数或者中文返回,需要使用借助System.Net.HttpWebRequest类
POST:
$url = #自行填写网页请求地址
$body=@
"user" = $user
"password"=$password
| ConvertTo-Json #具体参照接口参数修改
$webRequest = [System.Net.HttpWebRequest]::Create($url)
$encodedContent = [System.Text.Encoding]::UTF8.GetBytes($body)
$webRequest.Method = "POST"
$webRequest.ReadWriteTimeout = 5000
$webRequest.KeepAlive = $false
$webRequest.ContentType = "application/json"
$webRequest.ContentLength = $encodedContent.length
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($encodedContent, 0, $encodedContent.length)
$requestStream.Close()
[System.Net.WebResponse] $resp = $webRequest.GetResponse()
$rs = $resp.GetResponseStream()
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs
[string] $results = $sr.ReadToEnd()
GET:
$url = #自行填写网页请求地址
[Net.HttpWebRequest]$WebRequest = [Net.WebRequest]::Create($Uri)
[Net.HttpWebResponse]$WebResponse = $WebRequest.GetResponse()
$Reader = New-Object IO.StreamReader($WebResponse.GetResponseStream())
$Response = $Reader.ReadToEnd()
$Reader.Close()
$results = $Response | ConvertFrom-Json
想详细了解的可以看一下https://learn.microsoft.com/zh-cn/dotnet/api/system.net.httpwebrequest?redirectedfrom=MSDN&view=net-6.0
以上是关于powershell网页getpost 中文请求与返回的主要内容,如果未能解决你的问题,请参考以下文章