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!