1 public string GetPageContent(string strurl)
2 {
3 try
4 {
5 string ResponseText = String.Empty;
6 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strurl);
7 request.UserAgent = HttpContext.Current.Request.UserAgent;
8 WebResponse response = request.GetResponse();
9 Stream resStream = response.GetResponseStream();
10 StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
11 ResponseText = sr.ReadToEnd();
12 resStream.Close();
13 sr.Close();
14 return ResponseText;
15 }
16 catch (Exception ex)
17 {
18 throw ex;
19 }
20 }
上面这段采集代码为什么放在Global文件中进行定时采集的时候都会报
request.UserAgent 为空呢?
request.UserAgent 可以写死
request.UserAgent = "IE 6.0:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727)";
HttpContext.Current.Request.UserAgent没有值,
HttpContext.Current.Request是表示当前向你自己的网页发送的请求,假如你是通过自己的请求触发的向其它网站发起请求,就会有值,但是你现在是放在Global文件中定时执行的,HttpContext.Current.Request就会是空的。