使用Powershell / curl检查http标头的问题

时间:2013-01-08 20:02:10

标签: powershell curl command-prompt

事实证明这比我想象的要困难得多(而且可能应该是这样)。

我已经尝试过Cygwin + curl,但是无法运行(Cygwin永远无法找到curl.exe,但我确实做了一个包检查,它就在那里)。

我已经尝试使用命令提示符进行Windows的curl,并且它可以工作。但我有很多网址需要检查并单独进行,只是时间效率不高。我无法弄清楚如何通过cmd提示告诉curl使用一个文件(因为没有“xargs”),如提到here

我也尝试过使用PowerShell,但这也存在问题。当我尝试按照选项1 here

尝试运行$xHTTP.open("GET",$url,$false)时出错:

  

使用“3”参数调用“open”的异常:“未指定的错误(来自HRESULT的异常:0x80004005(E_FAIL))”

同样使用PowerShell,我完全不知道如何使用包含url的文件。我对PS的了解非常有限(如不存在的那样)。

这里最好的方法是弄清楚如何让命令提示符/ curl使用文件,但我无法理解。

1 个答案:

答案 0 :(得分:8)

使用PowerShell V3有一种更直接的方法:

PS> Get-Content .\urls.txt
http://www.cnn.com
http://www.msn.com

PS> Get-Content urls.txt | Foreach { Invoke-WebRequest -Uri $_ -Method HEAD }

StatusCode        : 200
StatusDescription : OK
Content           :
RawContent        : HTTP/1.1 200 OK
                    Vary: Accept-Encoding
                    Connection: Keep-Alive
                    Cache-Control: max-age=60, private
                    Content-Type: text/html
                    Date: Tue, 08 Jan 2013 20:21:46 GMT
                    Expires: Tue, 08 Jan 2013 20:22:46 GMT...
Forms             : {}
Headers           : {[Vary, Accept-Encoding], [Connection, Keep-Alive], [Cache-Control, max-age=60, private],
                    [Content-Type, text/html]...}
...

要处理404,请使用try / catch例如:

PS> Get-Content urls.txt | 
        Foreach {try {Invoke-WebRequest -Uri $_ -Method HEAD} catch { "Oops - $_"}}

要重定向到文件,这对我有用:

PS> Get-Content urls.txt | 
        Foreach {try {Invoke-WebRequest -Uri $_ -Method HEAD} catch { "Oops - $_"}} > 
        $home\Desktop\foo.txt