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 //Request2 stringURI =http://www.baidu.com;3HttpWebRequest request = Httpwebrequest.create (URI) asHttpWebRequest;4Request. Method ="GET";//Request Method5Request. ProtocolVersion =NewVersion (1,1);//http/1.1 Version
Add other ...
Receive responses, output response header information, and body information
1HttpWebResponse response=2Request. GetResponse () asHttpWebResponse;3 //Header4 foreach(varIteminchResponse. Headers)5 {6 This. txt_header.text + = Item. ToString () +": "+7 Response. getResponseHeader (item. ToString ())8+System.Environment.NewLine;9 }Ten One //receives the principal information content if the principal information is not empty A if(Response. ContentLength <=0) - return; - //Receive response body information the using(Stream stream =Response. GetResponseStream ()) - { - intTotallength= (int) Response. ContentLength; - intNumbytesread=0; + byte[] bytes=New byte[totallength+1024x768]; - //read the data in a stream through a loop, read it, jump out of the loop + while(Numbytesread <totallength) A { at intNum=stream. Read (Bytes,numbytesread,1024x768);//every time you want to read 1024 bytes - if(num==0)//indicates that the data in the stream is finished reading - Break; -numbytesread+=num; - }
28}
in //display the received principal data to the interface - stringContent=Encoding.UTF8.GetString (bytes); to 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 //...2 stringUrl="http://xx.xxx/xx.jpg";//URL of the picture resource3 //...4 using(Stream stream =Response. GetResponseStream ())5 {6 //current time as file name7 stringFileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS")+". jpg";8 using(Stream Fsstream =NewFileStream (FileName, FileMode.Create))9 {Ten Stream. CopyTo (fsstream); One } A }
Attached: Operating results such as:
C # Implementation adds a simple HTTP request