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