Intranet users or proxy users
Copy codeThe Code is as follows: using System. IO;
Using System. Net;
Public string get_html ()
{
String urlStr = "http://www.domain.com"; // specifies the URL to be retrieved
HttpWebRequest hwr = (HttpWebRequest) HttpWebRequest. Create (urlStr); // Create an HttpWebRequest object
Hwr. Timeout = 60000; // defines the time when the server exceeds the time limit.
WebProxy proxy = new WebProxy (); // defines a relational object
Proxy. Address = new Uri ("http://proxy.domain.com: 3128"); // web server: Port
Proxy. Credentials = new NetworkCredential ("f3210316", "6978233"); // use the secret name, secret
Hwr. UseDefaultCredentials = true; // users access the website
Hwr. Proxy = proxy; // set the network relationship
Try
{
HttpWebResponse hwrs = (HttpWebResponse) hwr. GetResponse (); // Get Response
}
Catch
{
MessageBox. Show ("unable to connect to proxy! ");
Return;
}
// Determine the HTTP response status
If (hwrs. StatusCode! = HttpStatusCode. OK)
{
MessageBox. Show ("Access failed! ");
Hwrs. Close ();
Return;
}
Else
{
Stream s = hwrs. GetResponseStream (); // get the response Stream Object
StreamReader sr = new StreamReader (s, Encoding. UTF8); // take the stream with UTF-8 too many bytes
StringBuilder content = new StringBuilder ();//
While (sr. Peek ()! =-1) // fetch a row for each commit
{// NO content exists for the next word
Content. Append (sr. ReadLine () + "" r "n"); // The end is returned.
}//
// Return content. ToString ();
}
// Output all headers (including the Cookie output by the server)
// For (int ii = 0; ii //{
// MessageBox. Show (hwrs. Headers. GetKey (ii) + ":" + res. Headers [ii]);
//}
}
As you know, HttpWebRequest can be used to capture webpages through Http. However, if the webpage is an intranet user and the webpage is accessed through a proxy, direct operations will not work.
Is there any way?
Of course, see the following code:Copy codeThe Code is as follows: string urlStr = "http://www.domain.com"; // set the URL to be retrieved
HttpWebRequest hwr = (HttpWebRequest) HttpWebRequest. Create (urlStr); // Create an HttpWebRequest object
Hwr. Timeout = 60000; // defines the time when the server exceeds the time limit.
WebProxy proxy = new WebProxy (); // defines a relational object
Proxy. Address = new Uri ("http://proxy.domain.com: 3128"); // web server: Port
Proxy. Credentials = new NetworkCredential ("f3210316", "6978233"); // use the secret name, secret
Hwr. UseDefaultCredentials = true; // users access the website
Hwr. Proxy = proxy; // set the network relationship
HttpWebResponse hwrs = (HttpWebResponse) hwr. GetResponse (); // Get Response
Stream s = hwrs. GetResponseStream (); // get the response Stream Object
StreamReader sr = new StreamReader (s, Encoding. UTF8); // take the stream with UTF-8 too many bytes
StringBuilder content = new StringBuilder ();//
While (sr. Peek ()! =-1) // fetch a row for each commit
{// NO content exists for the next word
Content. Append (sr. ReadLine () + "" r "n"); // The end is returned.
}//
Return content. ToString (); // return the string