2011-09-27 84 views
0

可能這是一個愚蠢的問題,但我到目前爲止在Google中找不到答案。 我需要閱讀頁面的HTML源代碼 http://Mysite/ViewRequest.aspx?request_id=xxx。 我設置以下Powershell - 將ASP頁面的源代碼分配給一個變量

wc = new-object -com internetexplorer.application 
$wc.visible = $false 
$wc.navigate2($strGRSpage).Document 
$wc.silent = $true 
$wc.visible = $false 
$GRSstr = $wc.Document.body.IHTMLElement_outerText 

不幸的是,IE頁面彈出,即使我設置$ wc.silent = $ true和$ wc.visible = $假的。 有沒有另一種方式(即使有HTTP請求GET)來檢索數據而無需獲取IE頁面。 注意。該網站http://Mysite/ViewRequest.aspx?request_id=xxx只支持IE和Mozilla。當我嘗試使用Webclient類時,我總是得到不受支持的瀏覽器。

在VBScript以下工作(但我想它在PowerShell中)

Set oHTML = CreateObject("Microsoft.XMLHTTP") 
Set oWeb = CreateObject("InternetExplorer.Application") 
oHTML.open "GET" , strGRSpage, false 
oHTML.send 
strGRStext = oHTML.responseText 

非常感謝,

馬爾科

回答

1

您只需添加一個合適的user-agent標題。

例如:

$wc = new-object system.net.webclient 
$wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") 
$wc.DownloadFile("http://whatsmyuseragent.com/","d:\index.html") 
+0

我已經嘗試指定用戶代理,但我得到了同樣的錯誤。 有沒有辦法模擬IE用戶代理? – user967598

1

當試圖WebClient類,也許你只是缺少添加一個「有效「用戶代理

$ wc.Headers.Add(」user-agent「,」Valid WebClient Header「)

0

谷歌搜索一點點後,我發現瞭如何設置我的用戶代理以獲得與IE瀏覽器的兼容性。

Final code is 
$wc = new-object system.net.webclient 
$wc.Headers.Add("user-agent", "Windows-RSS-Platform/2.0 (MSIE 9.0; Windows NT 6.1)") 
$GRSstr = $wc.DownloadString($strGRSpage) 

非常感謝您的幫助。

問候, 馬爾科

-----請標記爲答案------