简体   繁体   中英

GetAsync causing timeout issue

I have below code which causing issue while fetching data

 var result = client.GetAsync(requestUri).Result;

If I hit requestUri from browser then it works fine. but from above code it gives below error.

Error :

One or more errors occurred.An error occurred while sending the request.The underlying connection was closed: An unexpected error occurred on a send.Authentication failed because the remote party has closed the transport stream.

Stack Trace1 :

at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at foo(String resultUrl) at dooo(String param, String resultUrl) at foo.<>c__DisplayClass2.<Product>b__0(String x, String y) at foo[T](Func 3 fn) at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)

Stack Trace2

at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at Foo1(String resultUrl) at Foo2(String param, String resultUrl) at Foo3.<>c__DisplayClass2.<Product>b__0(String x, String y) at Foo4(Func 3 fn) at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()

Also i tried awaitable call but no luck. Can we increase request timeout for get request in this scenario.

Awaitable code I tried

var result = await client.GetAsync(requestUri);
                if (!result.IsSuccessStatusCode) throw new Exception(string.Format("Unable to receive data from Uri: {0}", uri.ToString()));

                // write the results
                using (var write = File.Create(filePath))
                using (var read = await result.Content.ReadAsStreamAsync())
                {
                    read.Seek(0, SeekOrigin.Begin);
                    read.CopyTo(write);
                }

If the response is talking long time and you want to increase the timeout just set it on the client: https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.timeout(v=vs.118).aspx

Also from looking at the class itself:

TimeSpan defaultTimeout = TimeSpan.FromSeconds(100.0);
TimeSpan maxTimeout = TimeSpan.FromMilliseconds((double) int.MaxValue);
TimeSpan infiniteTimeout = TimeSpan.FromMilliseconds(-1.0);

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