. Net 4.5 (C #): Uses httpclient to asynchronously send post requests and parse gzip responses

Source: Internet
Author: User

In. in the era of net 4.0 and C #4.0, if it is written asynchronously, it will write a lot of code, and I promise the code will not look good, because the result of a lot of APM asynchronous code execution is either a layer-by-layer nested lambda, or a piece of logic is forced to be split into a bunch of methods.

 

Another drawback is that the user must manually encode the URL parameter and connect it to the HTTP request correctly. multiple derived types of the httpcontent type in net 4.5 can support faster HTTP data content creation. We can use formurlencodedcontent to meet the above requirements.

 

The other is to automatically decompress the gzip in the HTTP response ,. in net 4.0, In the automaticdecompression attribute of httpwebrequest. in the httpclient type in. Net 4.5, you can use the automaticdecompression attribute of httpclienthandler. The values of the two before and after are both of the decompressionmethods enumeration types. NET 2.0. Therefore, the namespace is in system. net.

 

We can use an API for demonstration. The entire code is as follows:

// + Using system. net;

// + Using system. net. HTTP;

 

Static void main (string [] ARGs)

{

Doo ();

System. Threading. thread. Sleep (-1 );

}

 

Static async void doo ()

{

// Set necessary parameters

// Example API can be referred to: http://dev.jiepang.com/doc/get/users/show

VaR url = "http://api.jiepang.com/v1/users/show ";

VaR userid = "772653441 ";

VaR source = "mgen. orca ";

 

// Sets the automaticdecompression of httpclienthandler.

VaR handler = new httpclienthandler ()

{Automaticdecompression = decompressionmethods. gzip };

 

// Create httpclient (note that httpclienthandler is passed in)

Using (var http = new httpclient (handler ))

{

// Use formurlencodedcontent for httpcontent

VaR content = new formurlencodedcontent (new dictionary <string, string> ()

{

{"ID", userid },

{"Source", source },

{"Force_gzip", "1 "}

});

 

// Await waits for a response Asynchronously

VaR response = await HTTP. postasync (URL, content );

 

// Ensure the HTTP success status value

Response. ensuresuccessstatuscode ();

 

// Await asynchronously reads the final JSON file (note that gzip has been automatically decompressed because the above automaticdecompression = decompressionmethods. gzip)

Console. writeline (await response. content. readasstringasync ());

}

}

 

After running the command, if there is a network, the JSON information of the Account will be output!

 

The preceding API uses a display parameter to specify the response to gzip data to be returned. You can also set the HTTP request acceptencoding as follows and manually add the gzip type as follows:

// HTTP is an httpclient object

// You can also manually build httprequestmessage and send it through httpclient. sendasync.

HTTP. defaultrequestheaders. acceptencoding. Add (New stringwithqualityheadervalue ("gzip "));

 

HTTP is the httpclient object. Its defaultrequestheaders returns the httprequestheaders object. Of course, you can also manually build httprequestmessage, modify httprequestheaders, and then send the message through httpclient. sendasync.

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.