asp.net使用技巧(三)

如何下载网页?
首先使用 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;

posted on 2006-05-10 14:19  kim  阅读(715)  评论(1编辑  收藏  举报

导航