可能这是一个愚蠢的问题,但到目前为止我在谷歌找不到答案。 我需要阅读页面的HTML源代码 http://Mysite/ViewRequest.aspx?request_id=xxx。 我已经设置了以下
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 = $ false。 是否有另一种方法(即使使用HTTP请求GET)来检索该数据而不获取IE页面。 注意。网站http://Mysite/ViewRequest.aspx?request_id=xxx仅支持IE和Mozilla。当我尝试使用Webclient Class时,我总是得到不支持的浏览器。
在VBscript下面的作品(但我希望它在Powershell中)
Set oHTML = CreateObject("Microsoft.XMLHTTP")
Set oWeb = CreateObject("InternetExplorer.Application")
oHTML.open "GET" , strGRSpage, false
oHTML.send
strGRStext = oHTML.responseText
非常感谢,
马
答案 0 :(得分:1)
尝试使用webclient类时,您可能只是缺少添加“有效”用户代理
$ wc.Headers.Add(“user-agent”,“Valid WebClient Header”)
答案 1 :(得分: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")
答案 2 :(得分: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)
非常感谢你的帮助。
此致 马可
-----请标记为答案------