关于HttpWebRequest
朋友遇到了一个问题,使用WebRequest请求SSL页面时,有些页面不返回任何HTML代码,而其它页面都能。为了验证他的问题,我写了以下的测试代码:
1、网络代理
这段代码在公司完成,所以增加了代理设置,第2行到第10行,输入代理地址时,别忘了加http://
2、SSL(HTTPS)
一般情况下,我们直接为Credentials属性赋值即可,这方面可以参考MSDN的相关说明。
3、UserAgent
朋友的代码中没有对UserAgent的处理,多数的网站也能打开,我分析可能是网站程序为了安全性,屏蔽了非浏览器的请求,增加了UserAgent的处理后,解决了朋友的问题,请参考第13行代码。
4、HttpWebRequest和WebRequest
在处理页面时,还是建议使用HttpWebRequest,虽然WebRequest的功能更强一些,但HttpWebRequest针对HTTP处理会更方便。
1 HttpWebRequest request = (HttpWebRequest) WebRequest.Create(txtUrl.Text);
2 WebProxy proxy = (WebProxy) request.Proxy;
3
4 // proxy information
5 if (txtProxyUri.Text.Length != 0)
6 {
7 proxy.Address = new Uri(txtProxyUri.Text);
8 proxy.Credentials = new NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text);
9 }
10 request.Proxy = proxy;
11
12 request.AllowAutoRedirect = true;
13 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";
14 // centificate
15 request.PreAuthenticate = true;
16 request.Credentials = CredentialCache.DefaultCredentials;
17
18 // the following code copy from msdn
19 WebResponse response = request.GetResponse();
20 Stream streamResponse = response.GetResponseStream();
21 StreamReader streamRead = new StreamReader(streamResponse);
22 Char[] readBuff = new Char[256];
23 int count = streamRead.Read(readBuff, 0, 256);
24 Console.WriteLine("\nThe contents of the Html pages are :");
25 while (count > 0)
26 {
27 String outputData = new String(readBuff, 0, count);
28 Console.Write(outputData);
29 count = streamRead.Read(readBuff, 0, 256);
30 }
31 streamResponse.Close();
32 streamRead.Close();
33 response.Close();
34
2 WebProxy proxy = (WebProxy) request.Proxy;
3
4 // proxy information
5 if (txtProxyUri.Text.Length != 0)
6 {
7 proxy.Address = new Uri(txtProxyUri.Text);
8 proxy.Credentials = new NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text);
9 }
10 request.Proxy = proxy;
11
12 request.AllowAutoRedirect = true;
13 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";
14 // centificate
15 request.PreAuthenticate = true;
16 request.Credentials = CredentialCache.DefaultCredentials;
17
18 // the following code copy from msdn
19 WebResponse response = request.GetResponse();
20 Stream streamResponse = response.GetResponseStream();
21 StreamReader streamRead = new StreamReader(streamResponse);
22 Char[] readBuff = new Char[256];
23 int count = streamRead.Read(readBuff, 0, 256);
24 Console.WriteLine("\nThe contents of the Html pages are :");
25 while (count > 0)
26 {
27 String outputData = new String(readBuff, 0, count);
28 Console.Write(outputData);
29 count = streamRead.Read(readBuff, 0, 256);
30 }
31 streamResponse.Close();
32 streamRead.Close();
33 response.Close();
34
1、网络代理
这段代码在公司完成,所以增加了代理设置,第2行到第10行,输入代理地址时,别忘了加http://
2、SSL(HTTPS)
一般情况下,我们直接为Credentials属性赋值即可,这方面可以参考MSDN的相关说明。
3、UserAgent
朋友的代码中没有对UserAgent的处理,多数的网站也能打开,我分析可能是网站程序为了安全性,屏蔽了非浏览器的请求,增加了UserAgent的处理后,解决了朋友的问题,请参考第13行代码。
4、HttpWebRequest和WebRequest
在处理页面时,还是建议使用HttpWebRequest,虽然WebRequest的功能更强一些,但HttpWebRequest针对HTTP处理会更方便。