C # get three ways to get web content

C# usually has three methods to get web content, using WebClient, WebBrowser or HttpWebRequest/HttpWebResponse.

Method 1: Use WebClient

copy code
static void Main(string[] args)

{
try {
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//Get or set the network credentials used to authenticate requests to Internet resources
Byte[] pageData = MyWebClient.DownloadData("http://www.163.com"); //Download data from the specified website
string pageHtml = Encoding.Default.GetString(pageData); //If you use GB2312 to get the website page, use this sentence
//string pageHtml = Encoding.UTF8.GetString(pageData); //If you use UTF-8 to get the website page, use this sentence
Console.WriteLine(pageHtml);//Enter the obtained content in the console
using (StreamWriter sw = new StreamWriter("c:\\test\\ouput.html"))//write the obtained content into the text
{
sw.Write(pageHtml);
}
Console.ReadLine(); //Let the console pause, otherwise it will pass by in a flash
}
catch(WebException webEx) {
Console.WriteLine(webEx.Message.ToString());
}
}
copy code

Method 2: Use WebBrowser

Copy code
WebBrowser web = new WebBrowser();

web.Navigate("http://www.xjflcp.com/ssc/");
web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);
void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser web = (WebBrowser)sender;
HtmlElementCollection ElementCollection = web.Document.GetElementsByTagName("Table");
foreach (HtmlElement item in ElementCollection)
{
File.AppendAllText("Kaijiang_xj.txt", item.InnerText);
}
}
copy code

Method 3: Use HttpWebRequest/HttpWebResponse

 1 HttpWebRequest httpReq;

2 HttpWebResponse httpResp;
3
4 string strBuff = "";
5 char[] cbuffer = new char[256];
6 int byteRead = 0;
7
8 string filename = @"c:\log.txt";
9 ///Define write stream operation
10 public void WriteStream()
11 {
12 Uri httpURL = new Uri(txtURL.Text);
13
14 ///The HttpWebRequest class inherits from WebRequest and does not have its own constructor. It needs to be established through the Creat method of WebRequest, and forced type conversion is performed.
15 httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
16 ///Create HttpWebResponse through GetResponse() method of HttpWebRequest, and force type conversion
17
18 httpResp = (HttpWebResponse) httpReq.GetResponse();
19 ///The GetResponseStream() method gets the data stream of the HTTP response and tries to get the web page content specified in the URL
20
21 ///If the content of the webpage is successfully obtained, it will be returned in the form of System.IO.Stream. If it fails, a ProtoclViolationException error will be generated. The correct approach here is to put the following code in a try block for processing. Simple processing here
22 Stream respStream = httpResp.GetResponseStream();
23
24 ///The returned content is in the form of Stream, so you can use the StreamReader class to get the content of GetResponseStream and use it as a
25
26 The Read method of the StreamReader class reads the content of each line of the webpage source code in turn, Until the end of the line (code format read: UTF8)
27 StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8);
28
29 byteRead = respStreamReader.Read(cbuffer,0,256);
30
31 while (byteRead != 0)
32 {
33 string strResp = new string(cbuffer,0,byteRead);
34 strBuff = strBuff + strResp;
35 byteRead = respStreamReader.Read(cbuffer,0 ,256);
36 }
37
38 respStream.Close();
39 txtHTML.Text = strBuff;
40 }

< div class="code_toolbar"> copy code div>

static void Main(string[] args)

{
try {
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//Get or set the network credentials used to authenticate requests to Internet resources
Byte[] pageData = MyWebClient.DownloadData("http://www.163.com"); //Download data from the specified website
string pageHtml = Encoding.Default.GetString(pageData); //If you use GB2312 to get the website page, use this sentence
//string pageHtml = Encoding.UTF8.GetString(pageData); //If you use UTF-8 to get the website page, use this sentence
Console.WriteLine(pageHtml);//Enter the obtained content in the console
using (StreamWriter sw = new StreamWriter("c:\\test\\ouput.html"))//write the obtained content into the text
{
sw.Write(pageHtml);
}
Console.ReadLine(); //Let the console pause, otherwise it will pass by in a flash
}
catch(WebException webEx) {
Console.WriteLine(webEx.Message.ToString());
}
}
copy code

copy code

copy code

copy code
WebBrowser web = new WebBrowser();

web.Navigate("http://www.xjflcp.com/ssc/");
web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted);
void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser web = (WebBrowser)sender;
HtmlElementCollection ElementCollection = web.Document.GetElementsByTagName("Table");
foreach (HtmlElement item in ElementCollection)
{
File.AppendAllText("Kaijiang_xj.txt", item.InnerText);
}
}
copy code

copy code

copy code

 1 HttpWebRequest httpReq;

2 HttpWebResponse httpResp;
3
4 string strBuff = "";
5 char[] cbuffer = new char[256];
6 int byteRead = 0;
7
8 string filename = @"c:\log.txt";
9 ///Define write stream operation
10 public void WriteStream()
11 {
12 Uri httpURL = new Uri(txtURL.Text);
13
14 ///The HttpWebRequest class inherits from WebRequest and does not have its own constructor. It needs to be established through the Creat method of WebRequest, and forced type conversion is performed.
15 httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
16 ///Create HttpWebResponse through GetResponse() method of HttpWebRequest, and force type conversion
17
18 httpResp = (HttpWebResponse) httpReq.GetResponse();
19 ///The GetResponseStream() method gets the data stream of the HTTP response and tries to get the web page content specified in the URL
20
21 ///If the content of the webpage is successfully obtained, it will be returned in the form of System.IO.Stream. If it fails, a ProtoclViolationException error will be generated. The correct approach here is to put the following code in a try block for processing. Simple processing here
22 Stream respStream = httpResp.GetResponseStream();
23
24 ///The returned content is in the form of Stream, so you can use the StreamReader class to get the content of GetResponseStream and use it as a
25
26 The Read method of the StreamReader class reads the content of each line of the webpage source code in turn, Until the end of the line (code format read: UTF8)
27 StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8);
28
29 byteRead = respStreamReader.Read(cbuffer,0,256);
30
31 while (byteRead != 0)
32 {
33 string strResp = new string(cbuffer,0,byteRead);
34 strBuff = strBuff + strResp;
35 byteRead = respStreamReader.Read(cbuffer,0 ,256);
36 }
37
38 respStream.Close();
39 txtHTML.Text = strBuff;
40 }

 1 HttpWebRequest httpReq;

2 HttpWebResponse httpResp;
3
4 string strBuff = "";
5 char[] cbuffer = new char[256];
6 int byteRead = 0;
7
8 string filename = @"c:\log.txt";
9 ///Define write stream operation
10 public void WriteStream()
11 {
12 Uri httpURL = new Uri(txtURL.Text);
13
14 ///The HttpWebRequest class inherits from WebRequest and does not have its own constructor. It needs to be established through the Creat method of WebRequest, and forced type conversion is performed.
15 httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
16 ///Create HttpWebResponse through GetResponse() method of HttpWebRequest, and force type conversion
17
18 httpResp = (HttpWebResponse) httpReq.GetResponse();
19 ///The GetResponseStream() method gets the data stream of the HTTP response and tries to get the web page content specified in the URL
20
21 ///If the content of the webpage is successfully obtained, it will be returned in the form of System.IO.Stream. If it fails, a ProtoclViolationException error will be generated. The correct approach here is to put the following code in a try block for processing. Simple processing here
22 Stream respStream = httpResp.GetResponseStream();
23
24 ///The returned content is in the form of Stream, so you can use the StreamReader class to get the content of GetResponseStream and use it as a
25
26 The Read method of the StreamReader class reads the content of each line of the webpage source code in turn, Until the end of the line (code format read: UTF8)
27 StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8);
28
29 byteRead = respStreamReader.Read(cbuffer,0,256);
30
31 while (byteRead != 0)
32 {
33 string strResp = new string(cbuffer,0,byteRead);
34 strBuff = strBuff + strResp;
35 byteRead = respStreamReader.Read(cbuffer,0 ,256);
36 }
37
38 respStream.Close();
39 txtHTML.Text = strBuff;
40 }

 1 HttpWebRequest httpReq;

2 HttpWebResponse httpResp;
3
4 string strBuff = "";
5 char[] cbuffer = new char[256];
6 int byteRead = 0;
7
8 string filename = @"c:\log.txt";
9 ///Define write stream operation
10 public void WriteStream()
11 {
12 Uri httpURL = new Uri(txtURL.Text);
13
14 ///The HttpWebRequest class inherits from WebRequest and does not have its own constructor. It needs to be established through the Creat method of WebRequest, and forced type conversion is performed.
15 httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
16 ///Create HttpWebResponse through GetResponse() method of HttpWebRequest, and force type conversion
17
18 httpResp = (HttpWebResponse) httpReq.GetResponse();
19 ///The GetResponseStream() method gets the data stream of the HTTP response and tries to get the web page content specified in the URL
20
21 ///If the content of the webpage is successfully obtained, it will be returned in the form of System.IO.Stream. If it fails, a ProtoclViolationException error will be generated. The correct approach here is to put the following code in a try block for processing. Simple processing here
22 Stream respStream = httpResp.GetResponseStream();
23
24 ///The returned content is in the form of Stream, so you can use the StreamReader class to get the content of GetResponseStream and use it as a
25
26 The Read method of the StreamReader class reads the content of each line of the webpage source code in turn, Until the end of the line (code format read: UTF8)
27 StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8);
28
29 byteRead = respStreamReader.Read(cbuffer,0,256);
30
31 while (byteRead != 0)
32 {
33 string strResp = new string(cbuffer,0,byteRead);
34 strBuff = strBuff + strResp;
35 byteRead = respStreamReader.Read(cbuffer,0 ,256);
36 }
37
38 respStream.Close();
39 txtHTML.Text = strBuff;
40 }

Leave a Comment

Your email address will not be published.