进行HttpWebRequest时如何启用Javascript [英] How to Enable Javascript When Making an HttpWebRequest
问题描述
我需要调用一个包含JavaScript的网页.在页面底部,我有以下内容:
I need to call a web page that has javascript. At the bottom of the page I have the following:
<noscript>
<p>Javascript is not supported or enabled.</p>
</noscript>
当我这样发出HttpWebRequest请求时,很明显页面上的javascript没有执行.
When I make my HttpWebRequest request like so, it is clear that the javascript on the page did not execute.
Dim req As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(New Uri(url)), System.Net.HttpWebRequest)
' Add the current authentication cookie to the request
Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies(FormsAuthentication.FormsCookieName)
Dim authenticationCookie As New System.Net.Cookie(FormsAuthentication.FormsCookieName, cookie.Value, cookie.Path, HttpContext.Current.Request.Url.Authority)
req.CookieContainer = New System.Net.CookieContainer()
req.CookieContainer.Add(authenticationCookie)
req.MediaType = "PRINT"
req.Method = "GET"
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
Dim res As System.Net.WebResponse = req.GetResponse()
我该怎么办?如果javascript没有运行,该响应对我没有用.我想将输出转换为PDF.我想我需要一种方法来执行响应中包含的javascript,但是要在浏览器之外执行.
What can I do? The response is not useful to me if the javascript did not run. I want to convert the output into a PDF. I guess I need a way to execute the javascript that in included in the response, but do so outside of the browser.
谢谢.
推荐答案
您要转换什么输出?您只能抓取静态HTML,不能抓取经过JavaScript修改的DOM.
What output do you want to convert? You can only scrape the static HTML, not the JavaScript-modified DOM.
请记住,HttpWebRequest
不会解释JavaScript.
Remember that HttpWebRequest
does not interpret JavaScript.
这篇关于进行HttpWebRequest时如何启用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!