使用Powershell Invoke-WebRequest时设置所有标头

时间:2014-03-04 21:22:18

标签: powershell fiddler

我想从在线表单下载PDF文件。我用提琴手找出标题和帖子字段。但是,我无法使用Invoke-webrequest从Firefox获得与Powershell匹配的标题。

原始请求

POST http://somesight/SPLeam/EAMDisplayReportWithParamValues.asp HTTP/1.1   
Host: someserver
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0    
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    
Accept-Language: en-US,en;q=0.5    
Accept-Encoding: gzip, deflate
Referer: http://someserver/SPLeam/EAMPickParamValues.asp?plant=01&reportuser=reports&reportuserpw=pw&ReportName=SPLEAM%5CMNT+%2D+MM+Tracking+Rpt%2Erpt
Cookie: ASPSESDFSDIDSSQAQBQC=APHPHMNCHIECSFSSPOIKBDCAM`    
Connection: keep-alive    
Content-Type: application/x-www-form-urlencoded    
Content-Length: 132

Text0=12%2F1%2F2013&Text1=12%2F31%2F2013&Text2=MESGI&Drop2=MESCI&Text3=01&Text4=&Drop4=&Text5=R&Text6=N&cmdSaveParameters=Run+Report

POWERSHELL

POST http://someserver/SPLeam/EAMPickParamValues.asp?plant=01&reportuser=reports&reportuserpw=pw&ReportName=SPLEAM%5CMNT+%2D+MM+Tracking+Rpt%2Erpt HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.1; en-US) WindowsPowerShell/3.0
Content-Type: application/x-www-form-urlencoded
Host: someserver
Content-Length: 129
Connection: Keep-Alive
cmdSaveParameters=Run%2bReport&Text3=01&Text5=R&Text1=12%252F31%252F2013&Drop2=MI&Text2=MI&Text4=&Text6=N&Text0=12%252F1%252F2013

我把它作为 $f = invoke-webrequest -uri $url -UserAgent $useragent -method Post -Body $param

如何让Powershell的请求与Firefox发送的一致?

1 个答案:

答案 0 :(得分:0)

.NET的HTTPWebRequest对象具有AcceptReferer标题的单独属性;您必须手动设置这些属性,而不是尝试直接添加标题。

(这些标题限制部分出于安全目的;部分信任代码无法设置属性。)