尝试使用 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
原始内容
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
最佳答案
如果查看标题,内容类型为空,因此 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()
关于powershell - Invoke-WebRequest 返回某些地址的意外结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51333965/