我在Linux上使用Powershell Core v6-beta.5使用AppImage。有没有办法找出301重定向的“新”位置?
Invoke-WebRequest -Method HEAD http://SomethingThatThrows301.com/ -MaximumRedirection 0抛出一个错误(Response status code does not indicate success: 301 (Moved Permanently))。
虽然错误确实提到移动是301,但我仍然希望一个适当的对象告诉我,以及新的地址。
是否有办法这样做?
发布于 2017-08-09 04:03:30
如果忽略抛出的错误,则可以检查HTTP响应。新的URL将在位置标头中。
试试下面这样的方法。
$url="https://jigsaw.w3.org/HTTP/300/301.html"
$resp = Invoke-WebRequest -Method HEAD $url -MaximumRedirection 0 -ErrorAction Ignore
$code = $resp.StatusCode
Write-Output "URL: $url"
Write-Output "ErrorCode: $code"
if($code -eq 301) {
$loc = $resp.Headers.Location
Write-Output "New URL: $loc"
}https://stackoverflow.com/questions/45574479
复制相似问题