C # Add simple Http requests,
Using two classes in. NetHttpWebRequestClass,HttpWebResponseClass to implement Http request and Response Processing.
The first small test is to request the content of Baidu homepage (http://www.baidu.com), that is, to obtain the html content of Baidu homepage,
Steps:
1. Create an HttpWebRequest object through the WebRequest class, which can contain Http request information.
(I do not understand why this object is created through the parent WebRequest instead of an HttpWebRequest.
In the HttpWebRequest class, the constructor is:
Protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext );)
2. Setting the HttpWebRequest object is actually setting the information content of the Http request message.
3. Obtain the HttpWebResponse object from the HttpWebRequest object, which contains the Http response information.
4. Obtain the response header information and response body information from the response information.
Some implementation code is as follows:
Create an HttpWebRequest request and set the Request Message
// Request 2 string uri = http://www.baidu.com; 3 HttpWebRequest request = HttpWebRequest. create (uri) as HttpWebRequest; 4 request. method = "GET"; // request Method 5 request. protocolVersion = new Version (1, 1); // Http/1.1
// Add Other...
Receive the response and output the response header and body information.
1 HttpWebResponse response = 2 request. getResponse () as HttpWebResponse; 3 // Header 4 foreach (var item in response. headers) 5 {6 this.txt _ Header. text + = item. toString () + ":" + 7 response. getResponseHeader (item. toString () 8 + System. environment. newLine; 9} 10 11 // if the subject information is not empty, The system receives the body information 12 if (response. contentLength> 0) 13 {
14 // receive response body information 15 using (Stream stream = response. getResponseStream () 16 {17 int totalLength = (int) response. contentLength; 18 int numBytesRead = 0; 19 byte [] bytes = new byte [totalLength + 1024]; 20 // read the data in the stream through a loop. The read is complete, loop 21 while (numBytesRead <totalLength) 22 {23 int num = stream. read (bytes, numBytesRead, 1024); // each time you want to Read 1024 bytes, 24 if (num = 0) // indicates that the data in the stream has been Read for 25 break; 26 numBytesRead + = num; 27}
28 // display the received subject data to the interface
29 string content = Encoding. UTF8.GetString (bytes );
30 this.txt _ Content. Text = content;
31}
The second small test is to request an image from the Internet and save the image locally.
The implementation steps are very similar to that of the first small test. The request is sent to the server through the image url, and then
After receiving the response, the response body information is saved as a local image file. One difference is that
You need to save the subject content as a file, not displayed on the interface.
The key code is as follows:
1 //... 2 string url = "http://xx.xxx/xx.jpg"; // url of the image resource 3 //... 4 using (Stream stream = response. getResponseStream () 5 {6 // The current time as the file name 7 string fileName = DateTime. now. toString ("yyyyMMddhhmmss") + ". jpg "; 8 using (Stream fsStream = new FileStream (fileName, FileMode. create) 9 {10 stream. copyTo (fsStream); 11} 12}
Appendix: The running result is as follows: