Three ways to get web content in C #

Source: Internet
Author: User

C # typically has three ways to get web content, using WebClient, WebBrowser, or httpwebrequest/httpwebresponse ...

Method One: Use WebClient (referenced from: http://fbljava.blog.163.com/blog/static/265211742008712105145244/)

static void Main (string[] args)

{

try {

WebClient mywebclient = new WebClient ();


Mywebclient.credentials = credentialcache.defaultcredentials;//Gets or sets the network credentials that are used to authenticate requests to Internet resources

byte[] Pagedata = Mywebclient.downloaddata ("http://www.163.com"); Download data from a specified Web site

String pagehtml = Encoding.Default.GetString (pagedata); If you are using GB2312 to get a website page, use this sentence

String pagehtml = Encoding.UTF8.GetString (pagedata); If you are using UTF-8 to get a website page, use this sentence

Console.WriteLine (pagehtml);//Enter what you get in the console

using (StreamWriter SW = new StreamWriter ("c:\\test\\ouput.html")//writes the acquired content to the text

{

Sw. Write (pagehtml);

}

Console.ReadLine (); Put the console on hold, or it'll flash over.

}

catch (WebException webEx) {

Console.WriteLine (WebEx.Message.ToString ());

}

}

Method Two: Use WebBrowser (quoted from: http://topic.csdn.net/u/20091225/14/4ea221cd-4c1e-4931-a6db-1fd4ee7398ef.html)

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);
}
}

Method Three: Use Httpwebrequest/httpwebresponse (referenced from: http://hi.baidu.com/onlyafar/blog/item/7ac4c6bf92d4810019d81f98.html)

HttpWebRequest Httpreq;
HttpWebResponse Httpresp;

String strbuff = "";
char[] Cbuffer = new char[256];
int byteread = 0;

string filename = @ "C:\log.txt";
Defining write Stream operations
public void Writestream ()
{
Uri Httpurl = new Uri (Txturl.text);

The HttpWebRequest class inherits from the WebRequest and does not have its own constructor, which needs to be established by WebRequest's Creat method and forced type conversion
Httpreq = (HttpWebRequest) webrequest.create (Httpurl);
Establish HttpWebResponse by HttpWebRequest's GetResponse () method, forcing type conversions

Httpresp = (HttpWebResponse) httpreq.getresponse ();
The GetResponseStream () method gets the HTTP response's data stream and attempts to get the page content specified in the URL

If the content of the Web page is successfully obtained, it is returned in System.IO.Stream form, and if the failure results in a protoclviolationexception error. In this correct practice, the following code should be put into a try block for processing. Easy to handle here.
Stream Respstream = Httpresp.getresponsestream ();

The returned content is in stream form, so you can use the StreamReader class to get the contents of GetResponseStream and

The Read method of the StreamReader class reads the contents of each line of the source code of the Web page sequentially, up to the end of the line (read encoded format: UTF8)
StreamReader Respstreamreader = new StreamReader (RESPSTREAM,ENCODING.UTF8);

Byteread = Respstreamreader.read (cbuffer,0,256);

while (Byteread! = 0)
{
String strresp = new string (cbuffer,0,byteread);
Strbuff = Strbuff + strresp;
Byteread = Respstreamreader.read (cbuffer,0,256);
}

Respstream.close ();
Txthtml.text = Strbuff;
}

Three ways to get web content in C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.