开始写Blog读取工具时一直是直接访问,访问方式也比较简单:

在C#代码中设置Http访问代理服务器WebRequest _HWR = WebRequest.CreateDefault(new System.Uri(URL));
在C#代码中设置Http访问代理服务器
在C#代码中设置Http访问代理服务器WebResponse _HRS 
= _HWR.GetResponse() ;
在C#代码中设置Http访问代理服务器
在C#代码中设置Http访问代理服务器Stream ReceiveStream 
= _HRS.GetResponseStream();

但是最近打开工程一看,代码运行不正常,调试了一下,原来客户端代理被ITS封掉了在C#代码中设置Http访问代理服务器,只得去查找设置代理访问的方法,原来代码很简单,只需要加入下面代码即可。

在C#代码中设置Http访问代理服务器WebProxy _WP = new WebProxy(ProxyName,ProxyPort);
在C#代码中设置Http访问代理服务器            _WP.BypassProxyOnLocal 
= true;
在C#代码中设置Http访问代理服务器            ICredentials credentials 
= new NetworkCredential(UserName,UserKey,DomainName);
在C#代码中设置Http访问代理服务器            _WP.Credentials 
= credentials;
在C#代码中设置Http访问代理服务器
在C#代码中设置Http访问代理服务器            WebRequest _HWR 
= WebRequest.CreateDefault(new System.Uri(URL));
在C#代码中设置Http访问代理服务器            _HWR.Proxy 
= _WP;
在C#代码中设置Http访问代理服务器            WebResponse _HRS 
= _HWR.GetResponse() ;

当然,MSDN上对于_HWR.Proxy = _WP;还有另外一个使用方式
GlobalProxySelection.Select = _WP;其实也就是设置全局的代理服务,不需要再一一设置,抓取页面数据就比较简单了。

在C#代码中设置Http访问代理服务器Encoding encode = System.Text.Encoding.Default;
在C#代码中设置Http访问代理服务器            StreamReader sr 
= new StreamReader( ReceiveStream, encode );
在C#代码中设置Http访问代理服务器            
在C#代码中设置Http访问代理服务器            
string HTMLContent = "";
在C#代码中设置Http访问代理服务器            
在C#代码中设置Http访问代理服务器            Char[] read 
= new Char[256];
在C#代码中设置Http访问代理服务器            
int count = sr.Read( read, 0256 );
在C#代码中设置Http访问代理服务器            
while (count > 0
{
在C#代码中设置Http访问代理服务器                String str 
= new String(read, 0, count);
在C#代码中设置Http访问代理服务器                HTMLContent 
+= str;
在C#代码中设置Http访问代理服务器                count 
= sr.Read(read, 0256);
在C#代码中设置Http访问代理服务器            }

OK,这样就完成了代码设置我们的代理,直接通过程序来访问IE内容了~~在C#代码中设置Http访问代理服务器

相关文章:

  • 2022-12-23
  • 2021-08-21
  • 2021-12-29
  • 2021-11-13
  • 2021-05-30
  • 2022-12-23
  • 2022-02-17
猜你喜欢
  • 2022-02-04
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案