. net4.5 in httpclient use note points

Source: Internet
Author: User

The httpclinet in net4.5 is a very powerful class, but some interesting things have been found in the recent practical project application.

At first I used it this way:

            using (var client = new HttpClient ())            {            }

But found that compared to the traditional httprequest to slow a lot, and later access to data, found that httpclient should not be released every time, because the socket is not released in time, the need to httpclient as a static to use.

private static readonly HttpClient Client = new HttpClient ();

Again after the use of the process requires intensive send GET request, but always feel slow, with fiddler view, found that each request only 2 times, the code is controlled by Semaphoreslim semaphore, the maximum number is 10. And the computer is configured to R5 1600, the system is Win7 x64, according to the logic of 10 is no problem, considering whether because servicepointmanager.defaultconnectionlimit limit the number of concurrent, I modified the Servicepointmanager.defaultconnectionlimit the value of 100, run the program again to find the number of concurrent or 2, so on StackOverflow found this article:


According to the above article, It seems that httpclient is not complying with servicepointmanager.defaultconnectionlimit, and in dense applications httpclient both accuracy and efficiency are lower than the traditional multi-threaded httprequest. But is that really the case? If it is really more httprequest than the traditional efficiency of the bottom, then why the giant hard to create httpclient this class? And we can see in the above link, the questioner's code in the httpclient is to consume the body, and in HttpRequest there is no consumption body. With this question I started the test.

            var tasks = Enumerable.range (1, 511). Select (Async i =            {                await semaphoreslim.waitasync ();                Try                {                    var html = await Client.getstringasync ($ "http://www.fynas.com/ua/search?d=&b=&k=&page={i }");                    var doc = parser. Parse (HTML);                                        var tr = doc. Queryselectorall (". Table-bordered tr:not (: First-child) Td:nth-child (4)"). ToList ();                    foreach (var element in tr)                    {                        list. Enqueue (element. Textcontent.trim ());                    }                    Doc. Dispose ();                }                Finally                {                    semaphoreslim.release ();                }            });

The above code, is to collect a useragent Daquan website, and my httpclient and servicepointmanager.defaultconnectionlimit is defined as:

        Static program ()        {            servicepointmanager.defaultconnectionlimit =;        }        private static readonly HttpClient Client = new HttpClient (new Httpclienthandler () {cookiecontainer = new Cookiecontainer ( )});

After many experiments, I found that HttpClient is to comply with the concurrency of Servicepointmanager.defaultconnectionlimit, the default or 2, we look at it is not difficult to find in fact httpclient is priority over Servicepointmanager.defau Ltconnectionlimit set, that is to say httpclient than Servicepointmanager.defaultconnectionlimit to instantiate first, next I change the code to this:

        Static program ()        {            servicepointmanager.defaultconnectionlimit = +;            Client = new HttpClient (new Httpclienthandler () {cookiecontainer = new Cookiecontainer ()});        private static readonly HttpClient Client;

Then run again, open the Fiddler for monitoring, found that this time the program will be able to perform normal concurrent 10来 access.

And httpclient in the Httpmessagehandle is also a very interesting place, we can carry out the actual situation to packaging httpmessagehandle, such as the following code to achieve the failure of access to retry the function:

    public class Myhttphandle:delegatinghandler    {public        myhttphandle (Httpmessagehandler innerhandler): Base ( Innerhandler)        {        }        protected override async task

When instantiating httpclient, pass in our defined handle:

private static readonly HttpClient Client = new HttpClient (new Myhttphandle (Handler))

This results in a total of three accesses, where any successful return succeeds and returns the result of a third visit if the second access is unsuccessful.

. net4.5 in httpclient use note points

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.