简体   繁体   中英

HTTP Request PostAsync with HttpClient cancels request or Resource temporarily unavailable

I have already written a post about this problem. I make HTTP Get and Post requests at regular intervals and some of these requests return an error that crashes my program (because no try-catch). The error comes from this code:

using (var client = new HttpClient())
            {
                var response = await client.PostAsync(url, data);
                string result = response.Content.ReadAsStringAsync().Result;
                Console.WriteLine(result);
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Success send post : " + result);
                } else
                {
                    Console.WriteLine("Error send post");
                }
            }

This function crashe with 2 errors:

Unhandled Exception: System.OperationCanceledException: The operation was canceled.
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Net.Http.HttpClient.HandleFinishSendAsyncError(Exception e, CancellationTokenSource cts)
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
nov. 18 04:36:08 raspberrypi wescan[8530]:    at Common.WebApi.SendHttpPost(String url, StringContent data)
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__7_1(Object state)
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Threading.QueueUserWorkItemCallbackDefaultContext.<>c.<.cctor>b__5_0(Object state)
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
nov. 18 04:36:08 raspberrypi wescan[8530]: --- End of stack trace from previous location where exception was thrown ---
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Threading.QueueUserWorkItemCallbackDefaultContext.ExecuteWorkItem()
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Threading.ThreadPoolWorkQueue.Dispatch()
nov. 18 04:36:08 raspberrypi wescan[8530]:    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
nov. 18 04:36:09 raspberrypi wescan[8530]: System.OperationCanceledException: The operation was canceled.
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Net.Http.HttpClient.HandleFinishSendAsyncError(Exception e, CancellationTokenSource cts)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at Common.WebApi.SendHttpPost(String url, StringContent data)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__7_1(Object state)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.QueueUserWorkItemCallbackDefaultContext.<>c.<.cctor>b__5_0(Object state)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
nov. 18 04:36:09 raspberrypi wescan[8530]: --- End of stack trace from previous location where exception was thrown ---
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.QueueUserWorkItemCallbackDefaultContext.ExecuteWorkItem()
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.ThreadPoolWorkQueue.Dispatch()
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
nov. 18 04:36:09 raspberrypi wescan[8530]: System.OperationCanceledException: The operation was canceled.
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Net.Http.HttpClient.HandleFinishSendAsyncError(Exception e, CancellationTokenSource cts)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at Common.WebApi.SendHttpPost(String url, StringContent data)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__7_1(Object state)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.QueueUserWorkItemCallbackDefaultContext.<>c.<.cctor>b__5_0(Object state)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
nov. 18 04:36:09 raspberrypi wescan[8530]: --- End of stack trace from previous location where exception was thrown ---
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.QueueUserWorkItemCallbackDefaultContext.ExecuteWorkItem()
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading.ThreadPoolWorkQueue.Dispatch()
nov. 18 04:36:09 raspberrypi wescan[8530]:    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

2

Unhandled Exception: System.Net.Http.HttpRequestException: Resource temporarily unavailable ---> System.Net.Sockets.SocketException: Resource temporarily unavailable
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
nov. 19 04:00:51 raspberrypi wescan[14169]:    --- End of inner exception stack trace ---
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading.Tasks.ValueTask`1.get_Result()
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading.Tasks.ValueTask`1.get_Result()
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading.Tasks.ValueTask`1.get_Result()
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at Common.WebApi.SendHttpPost(String url, StringContent data)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__7_1(Object state)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading.QueueUserWorkItemCallbackDefaultContext.<>c.<.cctor>b__5_0(Object state)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
nov. 19 04:00:51 raspberrypi wescan[14169]: --- End of stack trace from previous location where exception was thrown ---
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading.QueueUserWorkItemCallbackDefaultContext.ExecuteWorkItem()
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading.ThreadPoolWorkQueue.Dispatch()
nov. 19 04:00:51 raspberrypi wescan[14169]:    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

I found someone tell to change response.Content.ReadAsStringAsync().Result; to response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); but i can't test it for the moment. If anyone has had this kind of randomly generated error, I'm interested! cya

Both errors looks like connection/network related errors. Perhaps there is a temporary loss of connection to the endpoint. It could be because you are sending to many requests and getting throttled.

You don't state why you are not wrapping the call in a try/catch and just handle the exception. I would suggest doing that to make the code more robust.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM