Use C # To compile a webpage Capture Application)

Source: Internet
Author: User
This article uses the classes provided by C # And. Net to easily create a webpage content capture Source code Of Program . HTTP is one of the most basic protocols for WWW Data Access. NET provides two object classes: httpwebrequest and httpwebresponse, which are used to send requests and obtain responses to a resource respectively. To get the content of a resource, we first specify a URL address to be crawled, use the httpwebrequest object for request, and use the httpwebresponse object to receive response results, finally, use the textstream object to extract the information we want and print it out on the console.
The following describes how to implement this function:

Step 1: Open vs. net, click "file"-"New"-"project", select "Visual C # project" as the project type, and select "Windows application" as the template ",
Step 2: add the label1, button1, textbox1, and textbox2 controls to form1, and change the multiline attribute of textbox2 to true,
Step 3: Right-click the form1 form and choose ViewCode", And then enter:

Using system. IO;
Using system. net;
Using system. text;
In
Private void button#click (Object sender,
System. eventargs E)
{

}
Enter the following code:

Byte [] Buf = new byte [1, 38192];
Httpwebrequest request = (httpwebrequest)
Webrequest. Create (textbox1.text );
Httpwebresponse response = (httpwebresponse)
Request. getresponse ();
Stream resstream = response. getresponsestream ();
Int COUNT = resstream. Read (BUF, 0, Buf. Length );
Textbox2.text = encoding. Default. getstring (BUF, 0,
Count );
Resstream. Close ();

Step 4: click "Save all" and press "F5" to run the application. In the single-line text box after "Enter URL address:", enter http://lucky.myrice.com/down.htmand click "HTML code, the code for this address is displayed!

Next, we will analyze the above program:

The function of the above program is to capture the webpage token. First, we instantiate the httpwebrequest object and use the static method create () of the webrequest class. The string parameter of this method is the URL address of the page to be requested () the method returns the webrequest type. We must shape it (that is, type conversion) to the httpwebrequest type, and then assign it to the request variable. Once an httpwebrequest object is created, you can use its getresponse () method to return a webresponse object, and then form it into an httpwebresponse object and assign it to the response variable. Now, you can use
The getresponsestream () method is used to obtain the response text stream. Finally, the returned response information is put in the byte array Buf we initially created using the read () method of the stream object. Read () there are three parameters: the byte array to be placed, the starting position of the byte array, and the length of the byte array. Finally, convert the byte into a string. Note: The default encoding is used here. It uses the default encoding method, so we do not need to convert the character encoding. You can also use webrequest and webresponse to implement the above functions. The Code is as follows:

Webrequest request = webrequest. Create (textbox1.text );
Webresponse response = request. getresponse ();

Enter other URLs to see if they are convenient!

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.