We know. NET class library provides classes such as HttpWebRequest to facilitate our programming and Web server interaction. But in actual use we often encounter the following requirements, the underlying class does not directly provide the appropriate functionality:
The HTML obtained by HttpWebResponse is converted into text encoding so that it will not appear garbled;
Automatically maintain cookie,referer and other related information between sessions;
Simulate HTML form submission;
Uploading files to the server;
For binary resources, get the returned byte array directly (byte[]), or save as a file
To solve these problems, I developed the HttpClient class. The following are the methods used:
Gets the string after the encoding conversion
HttpClient client=new httpclient (URL);
String html=client. GetString ();
The GetString () function looks for HTTP Headers, as well as HTML meta tags, to try to find the encoded information for the retrieved content. If you can't find it, it uses the client. Defaultencoding, this property defaults to Utf-8, or it can be set manually.
Automatically keep cookies, Referer
HttpClient client=new httpclient (URL1, NULL, TRUE);
String html1=client. GetString ();
Client. URL=URL2;
String html2=client. GetString ();