How do I download a webpage?
First, use the System. Net. WebRequestFactory class to obtain a WebRequest object:
WebRequest request = WebRequestFactory. Create ("http: // localhost ");
Then the request is answered:
WebResponse response = request. GetResponse ();
The GetResponse method is blocked until the download is complete. Then you can access the response stream as follows:
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 );
Note that the WebRequest and WebReponse objects are backward compatible with HttpWebRequest and HttpWebReponse objects, which are used to access http-related functions.
How to use proxy )?
Two methods-this can affect all Web requests:
System. Net. GlobalProxySelection. Select = new DefaultControlObject ("proxyname", 80 );
In addition, to set proxy services for specific Web requests, do the following:
ProxyData proxyData = new ProxyData ();
ProxyData. HostName = "proxyname ";
ProxyData. Port = 80;
ProxyData. OverrideSelectProxy = true;
HttpWebRequest request = (HttpWebRequest) WebRequestFactory. Create ("http: // localhost ");
Request. Proxy = proxyData;