如何下载网页?
首先使用 System.Net.WebRequestFactory 类来获得一个 WebRequest 对象:
WebRequest request = WebRequestFactory.Create( "http://localhost" );
然后请求应答:
WebResponse response = request.GetResponse();
GetResponse 方法被阻塞直到下载完成。然后你能像下面那样访问应答流:
Stream s = response.GetResponseStream();
// Output the downloaded stream to the console
StreamReader sr = new StreamReader( s );
string line;
while( (line = sr.ReadLine()) != null )
Console.WriteLine( line );
注意 WebRequest 和 WebReponse 对象分别向下兼容 HttpWebRequest 和 HttpWebReponse 对象,它们被用来访问和 http 相关的功能。

如何使用代理服务器 (proxy)?
两种—这样做以便影响所有 Web 请求:
System.Net.GlobalProxySelection.Select = new DefaultControlObject( "proxyname", 80 );
另外一种,要想对特定的 Web 请求设置代理服务,这样做:
ProxyData proxyData = new ProxyData();
proxyData.HostName = "proxyname";
proxyData.Port = 80;
proxyData.OverrideSelectProxy = true;
HttpWebRequest request = (HttpWebRequest)WebRequestFactory.Create( "http://localhost" );
request.Proxy = proxyData;

相关文章:

  • 2021-06-28
  • 2021-08-20
  • 2021-07-13
  • 2021-08-02
  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-26
  • 2021-08-21
  • 2022-01-16
  • 2021-08-27
  • 2021-05-20
相关资源
相似解决方案