Public static async TaskPostAsync (string url, object data, Dictionary Headers=null)
{
Using (HttpClient Client=new HttpClient ())
{
If (headers!=null)
{
The foreach (KeyValuePairThe header in headers)
{
Client. DefaultRequestHeaders. Add (header. The Key, the header. The Value);
}
}
Try
{
The string content=data. The ToJson ();
Var buffer.=Encoding UTF8. GetBytes (content);
Var byteContent=new ByteArrayContent (buffer);
ByteContent. Headers. ContentType=new MediaTypeHeaderValue (" application/json ");
Var response=await Client. PostAsync (url, byteContent). ConfigureAwait (false);//run to this flash back directly, without any exception
The string result=await the response. The Content. ReadAsStringAsync ();
If (the response StatusCode!=HttpStatusCode. OK)
{
Return a string. The Empty;
}
return result;
}
The catch (Exception ex)
{
throw ex;
}
}
}
The second method
Public static async TaskHttpPostAsync (url string, the string postData Dictionary Headers=null string contentType=null, int timeout=0, Encoding Encoding=null)
{
Using (HttpClient client=new HttpClient ())
{
If (headers!=null)
{
The foreach (KeyValuePairThe header in headers)
{
Client. DefaultRequestHeaders. Add (header. The Key, the header. The Value);
}
}
If (a timeout & gt; 0)
{
Client. The Timeout=new TimeSpan (0, 0, the Timeout);
}
Using (HttpContent content=new StringContent (postData?? "", encoding?? . Encoding UTF8))
{
If (contentType!=null)
{
Content. The Headers. ContentType=new System.Net.Http.Headers.MediaTypeHeaderValue (ContentType);
}
Using (HttpResponseMessage responseMessage=await client. PostAsync (url, content))//run to this flash back directly, without any exception
{
Byte [] resultBytes=await responseMessage. Content. ReadAsByteArrayAsync ();
Return. Encoding UTF8. Get string (resultBytes);
}
}
}
}
This is why why why, or how to debug this problem
CodePudding user response:
Well, there is nothing wrong with this, the somebody else just tell you I do playJob done, don't let go, you GuanFan, there as long as you don't output XXXX is unusual, don't tube him,
Thread finished back, don't let the system to keep thousands of finish things have expired threads
?
CodePudding user response:
Run to the annotation that departed, the rest of the code is not executed!