HttpClient, using C # to manipulate the Web

Source: Internet
Author: User

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.