C # Webapi encounters a second request failure problem with a pit

Source: Internet
Author: User

Recently done a WEBAPI project that requires access to someone else's interface. After writing the interface to access the POST request, the debug succeeds!

But, the second request will time out! Why???

So all kinds of online search solutions, helpless is not found. What do we do? The HttpWebRequest class is then set aside for each attribute to see if there is any effect.

Emperor not bear, ah, finally found the crux of the disease where

No nonsense, sticker code:

1         /// <summary>2         ///POST Request3         /// </summary>4         /// <param name= "url" >Address</param>5         /// <param name= "Parameters" >Parameters</param>6         /// <param name= "TimeOut" >Timeout period</param>7         /// <returns></returns>8          Public Static stringHttpPost (stringURL, idictionary<string,string> Parameters,inttimeOut)9         {TenHttpWebRequest request =NULL; OneRequest = WebRequest.Create (URL) asHttpWebRequest; ARequest. Method ="POST"; -Request. ContentType ="application/x-www-form-urlencoded"; -Request. Timeout = timeout;//Timeout period theRequest. Servicepoint.expect100continue =false;//resolve the second request failure issue -  -             //Send post Data -             if(! (Parameters = =NULL|| Parameters. Count = =0)) +             { -StringBuilder buffer =NewStringBuilder (); +                 inti =0; A                 foreach(stringKeyinchparameters. Keys) at                 { -                     if(I >0) -                     { -Buffer. AppendFormat ("&{0}={1}", Key, Parameters[key]); -                     } -                     Else in                     { -Buffer. AppendFormat ("{0}={1}", Key, Parameters[key]); toi++; +                     } -                 } the                 byte[] data =Encoding.ASCII.GetBytes (buffer. ToString ()); *                 using(Stream stream =request. GetRequestStream ()) $                 {Panax NotoginsengStream. Write (data,0, data. Length); -                 } the             } +             string[] values = Request. Headers.getvalues ("Content-type"); A  theHttpWebResponse response =(HttpWebResponse) request. GetResponse (); +  -             using(Stream s =Response. GetResponseStream ()) $             { $StreamReader reader =NewStreamReader (S, Encoding.UTF8); -                 returnReader. ReadToEnd (); -             } the}

The key point is to set a property to false:request. ServicePoint. Expect100continue = false;

C # Webapi encounters a second request failure problem with a pit

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.