Powershell - assign source code of ASP page to a variable
probably it is a silly question, but I couldn't find the answer in Google so far. I need to read the HTML source code of a page http://Mysite/ViewRequest.aspx?request_id=xxx. I've setup following
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
Unfortunately the IE page pops up, even if I set $wc.silent = $true and $wc.visible = $false. Is there another way (even with HTTP request GET) to retrieve that data without getting the IE page. NOTE. The site http://Mysite/ViewRequest.aspx?request_id=xxx supports just IE and Mozilla. When I tried to use Webclient Class I get always unsupported browser.
In VBscript following works (but I want it i开发者_开发百科n Powershell)
Set oHTML = CreateObject("Microsoft.XMLHTTP")
Set oWeb = CreateObject("InternetExplorer.Application")
oHTML.open "GET" , strGRSpage, false
oHTML.send
strGRStext = oHTML.responseText
Many thanks,
Marco
You just have to add an appropriate user-agent
header.
For example:
$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")
When trying the webclient Class maybe You're just missing to add a "valid" user Agent
$wc.Headers.Add("user-agent", "Valid WebClient Header")
After Googling a little bit I found out how to setup my user agent to get the compatibility with 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)
Many thanks for your help.
Regards, Marco
----- Please mark as answer ------
精彩评论