Joe*_*Rod 5 powershell webrequest non-ascii-characters
我正在使用Invoke-RestMethod从我正在使用的应用程序中获取页面名称.我注意到当我在页面上执行GET时,它会返回页面名称
这个页面正在运作
但是实际的页面名称是
这个页面 - 正在运作
这是我的请求的样子
Invoke-WebRequest -Uri ("https://example.com/rest/api/content/123789") -Method Get -Headers $Credentials -ContentType "application/json; charset=utf-8"
Run Code Online (Sandbox Code Playgroud)
问题出在en-dash上,是否有人知道如何解决这个问题?
vit*_*ilo 11
如果Invoke-WebRequest没有检测到响应编码权限,您可以使用RawContentStream并将其转换为所需的编码:
$resp = Invoke-WebRequest -Uri ...
$html=[system.Text.Encoding]::UTF8.GetString($resp.RawContentStream.ToArray());
调用-restmethod 还是调用-webrequest?
Invoke-RestMethod cmdlet 对 HttpWebResponse.CharacterSet 属性的结果使用默认解码。
如果未设置,则默认使用默认编码 ISO-8859-1(据我所知)。
我假设您的服务器在响应标头中发送了一些错误的字符集(或删除了它),因此它被错误地解码。
您知道服务器响应中发送的字符集/编码是什么吗?
如果您正在尝试 Invoke-webrequest;检查您的回复中的标题,例如
$r = invoke-webrequest http://example.com
$r.Headers
Run Code Online (Sandbox Code Playgroud)
如果您正在处理编码问题;例如,您的服务器没有发送正确的标头;您始终可以尝试将响应转储到文件中并使用不同的编码读取它:
Invoke-WebRequest http://example.com -outfile .\test.txt
$content = get-content .\test.txt -Encoding utf8 -raw
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您将不再使用 http-response;但它可能会帮助您调试/查找您正在寻找的编码问题。
| 归档时间: |
|
| 查看次数: |
5524 次 |
| 最近记录: |