简体   繁体   English

GetAsync导致超时问题

[英]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. 如果我从浏览器中命中了requestUri ,那么它将正常工作。 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 : 堆栈跟踪1:

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) 在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 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)的System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)的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) .ConnectStream.WriteHeadersCallback(IAsyncResult ar)

Stack Trace2 堆栈跟踪2

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 在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 System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)位于System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)位于System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 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)。 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()上的WriteHeadersCallback(IAsyncResult ar)-从上次引发异常的位置开始的堆栈跟踪-在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在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() 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() System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()---从上一个引发异常的位置开始的堆栈跟踪结束--- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)位于System.Runtime System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()位于System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()处的.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

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 如果响应时间很长,并且您想增加超时时间,则只需在客户端上进行设置即可: https : //msdn.microsoft.com/zh-cn/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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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