Pass. NET of two classes HttpWebRequest class, HttpWebResponse class to implement HTTP request, response processing.
The first small test is to request the content of Baidu homepage (http://www.baidu.com), that is, to get the HTML content of Baidu homepage,
Implementation steps:
1. Create an HttpWebRequest object from the WebRequest class that can contain HTTP request information.
(What I don't understand is why this object is created through the parent class WebRequest, not the new one HttpWebRequest to create
, the constructors in the HttpWebRequest class are:
Protected HttpWebRequest (SerializationInfo SerializationInfo, StreamingContext StreamingContext); )
2. Setting the HttpWebRequest object is actually the information content of the HTTP request message.
3. Gets the HttpWebResponse object from the HttpWebRequest object that contains the HTTP response information.
4. Get response header information and response body information from the response information.
Some implementation codes are as follows:
Create HttpWebRequest request, set request message information
1//Request2String uri =http//www.baidu.com;3 HttpWebRequest request = Httpwebrequest.create (URI)AsHttpWebRequest;4 request. Method = " Get "; // Request method 5 request. ProtocolVersion = new Version ( 1, 1); // http/1.1 version
//add other ...
Receive responses, output response header information, and body information
1 HttpWebResponse response= 2 request. GetResponse ()AsHttpWebResponse; 3//Header 4foreach (VAR itemInchResponse. Headers) 5{ 6This.txt_Header.Text + = Item. ToString () +":" + 7Response. getResponseHeader (item. ToString ()) 8 +System.Environment.NewLine; 9}1011//Receives the principal information content if the principal information is not empty12if (response. ContentLength <=0)13Return;14//Receive Response body information15using (Stream stream =Response. GetResponseStream ())16{17int totallength= (Int) Response. ContentLength;18int numbytesread=0;19Byte[] Bytes=Newbyte[totallength+1024];20//Read the data in a stream through a loop, read it, jump out of the loop21stwhile (Numbytesread <Totallength)22{23int Num=stream. Read (Bytes,numbytesread,1024);//Every time you want to read 1024 bytes24if (num==0)//Indicates that the data in the stream is finished reading25break; 26 numbytesread+=num; 27 }
28}
29 // The received principal data is displayed to the interface 30 string content=encoding.utf8.getstring (bytes); 31 This.txt_content.text=content
The second small test is to request a picture on the web and save the picture locally.
The implementation steps are very similar to the first small test, which requests the server through the URL of the picture, and then
Receives the response, and the content of the response's principal information is saved as a local picture file. A little bit different is
The main content needs to be saved as a file, not displayed on the interface.
The key code is as follows:
1//... 2String url="Http://xx.xxx/xx.jpg";//URL of the picture resource 3//... 4using (Stream stream =Response. GetResponseStream ()) 5{ 6//Current time as file name 7String fileName = DateTime.Now.ToString ("Yyyymmddhhmmss")+ ".jpg "; 8 using (Stream fsstream = new FileStream (FileName, FileMode.Create) 9 {10 stream. CopyTo (Fsstream); 11 } 12 }
C # Implementation adds a simple HTTP request