在尝试使用Invoke-WebRequest
下面的地址是我遇到的几个例子之一。
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
(Invoke-WebRequest https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt).Content
(Invoke-WebRequest https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt).RawContent
Content属性似乎仅返回数字数组。 RawContent显示的结果更接近我的预期。
这是什么原因?
内容
35
32
65
100
32
102
105
108
116
101
RawContent
HTTP/1.1 200 OK
x-amz-id-2: juI0bRXZacf19D5/sjmsw53XudBjlbSYtO0XKVQP0N+dWXvixKoWs8mV8oF23SLt8iGjaE/CKco=
x-amz-request-id: 5B68B280AD366CDE
Accept-Ranges: bytes
Content-Length: 43642
Content-Type:
Date: Fri, 13 Jul 2018 23:25:09 GMT
ETag: "0a43bd5ebd9b3e2ee08a205d163cb0ac"
Last-Modified: Wed, 09 Mar 2016 19:46:05 GMT
Server: AmazonS3
# Ad filter list by Disconnect
# License: GPLv3
# Contact: support [at] disconnect.me
htmlcontent
答案 0 :(得分:4)
如果您查看标头,则“内容类型”为空,因此Invoke-WebRequest
不知道它在处理什么,并将数据视为二进制数据(字节数组)。
(Invoke-WebRequest https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt).Headers
Key Value
--- -----
Accept-Ranges bytes
Content-Length 43642
Content-Type
Date Sat, 14 Jul 2018 02:28:36 GMT
ETag "0a43bd5ebd9b3e2ee08a205d163cb0ac"
Last-Modified Wed, 09 Mar 2016 19:46:05 GMT
Server AmazonS3
例如,在此问题上致电Invoke-WebRequest
:
(Invoke-WebRequest https://stackoverflow.com/questions/51333965/invoke-webrequest-returning-unexpecte
d-results-for-some-addresses).Headers
Key Value
--- -----
X-Frame-Options SAMEORIGIN
X-Request-Guid 9366c316-b9b3-467b-8911-b18bb4752e1c
Strict-Transport-Security max-age=15552000
Content-Security-Policy upgrade-insecure-requests
Age 0,0
Accept-Ranges bytes,bytes
Content-Length 108783
Cache-Control private
Content-Type text/html; charset=utf-8
…
编辑:如果要否决内容类型,可以执行以下操作:
$url = 'https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt'
[System.Net.HttpWebRequest]$req = [System.Net.WebRequest]::Create($url) -as [System.Net.HttpWebRequest]
[System.Net.HttpWebResponse]$resp = $req.getResponse()
$respStream = $resp.getResponseStream()
$sr = new-object IO.StreamReader($respStream)
$result = $sr.ReadToEnd()
$resp.Close()