This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19711|Reply: 0

[Communication] C# to get the source code of the web page

[Copy link]
Posted on 2/15/2015 2:35:51 PM | | |
C# can be obtained in three ways: WebClient, WebRequest, and HttpWebRequest.

Of course, you can also use webBrowse! I won't look at how webBrowse gets here.

WebClient

private string GetWebClient(string url)
{
    string strHTML = "";
    WebClient myWebClient = new WebClient();
    Stream myStream = myWebClient.OpenRead(url);
    StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
    strHTML = sr. ReadToEnd();
    myStream.Close();
    return strHTML;
}
WebRequest

private string GetWebRequest(string url)
{
    Uri uri = new Uri(url);
    WebRequest myReq = WebRequest.Create(uri);
    WebResponse result = myReq.GetResponse();
    Stream receviceStream = result. GetResponseStream();
    StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("utf-8"));
    string strHTML = readerOfStream.ReadToEnd();
    readerOfStream.Close();
    receviceStream.Close();
    result. Close();
    return strHTML;
}
HttpWebRequest

private string GetHttpWebRequest(string url)
{
    Uri uri = new Uri(url);
    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
    myReq.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
    myReq.Accept = "*/*";
    myReq.KeepAlive = true;
    myReq.Headers.Add("Accept-Language", "zh-cn,en-us; q=0.5");
    HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
    Stream receviceStream = result. GetResponseStream();
    StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("utf-8"));
    string strHTML = readerOfStream.ReadToEnd();
    readerOfStream.Close();
    receviceStream.Close();
    result. Close();
    return strHTML;
}
Note that "UTF-8" should correspond to the encoding of the specified web page.

summary

You can see that the HttpWebRequest method is the most complex, but it does offer more choice.




Previous:Android mobile phone Alipay red envelope grabbing assistant script
Next:Command to view system resource usage in CentOS
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com