Home > Net >  HttpClient PostAsync asynchronous multi-threaded flash back without exception
HttpClient PostAsync asynchronous multi-threaded flash back without exception

Time:09-22

The first method
 
Public static async Task PostAsync (string url, object data, Dictionary Headers=null)
{
Using (HttpClient Client=new HttpClient ())
{
If (headers!=null)
{
The foreach (KeyValuePair The 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 Task HttpPostAsync (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 (KeyValuePair The 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 play

Job 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!

reference 1st floor wanghui0380 response:
, there is nothing wrong with this, the somebody else just tell you what I do to play

Job 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:

Exit without execution, thread to forget to write down. Wait ();
refer to the second floor fayxue response:
run to the annotation that departed, the rest of the code is not executed!

Quote: refer to 1st floor wanghui0380 response:

Well, there is nothing wrong with this, the somebody else just tell you I do play

Job 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?
  •  Tags:  
  • C#
  • Related