首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导致超时问题的GetAsync

导致超时问题的GetAsync
EN

Stack Overflow用户
提问于 2016-08-31 13:37:39
回答 1查看 1.2K关注 0票数 0

下面的代码会在获取数据时引发问题

代码语言:javascript
复制
 var result = client.GetAsync(requestUri).Result;

如果我从浏览器中点击requestUri,那么它就能正常工作。但是从上面的代码来看,它给出了以下错误。

错误:

发送基础连接关闭时发生一个或多个错误occurred.An错误:由于远程方关闭了传输流,send.Authentication上发生意外错误。

堆栈Trace1:

在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的System.Threading.Tasks.Task1.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](Func3 fn) )在System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)在System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)

堆栈Trace2

在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult的System.Threading.Tasks.Task1.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(Func3 fn),在System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()的System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) (System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) )上--从抛出异常的以前位置的堆栈跟踪--在System.Runtime上。( CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)在System.Runtime.CompilerServices.TaskAwaiter1.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.TaskAwaiter1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext() -从抛出异常的前一个位置开始的堆栈跟踪-在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务中抛出System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task任务)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()

另外,我试着打了个电话,但没有运气。在这种情况下,我们能增加get请求的请求超时吗?

我尝试过的代码

代码语言:javascript
复制
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);
                }
EN

回答 1

Stack Overflow用户

发布于 2016-08-31 14:51:29

如果响应时间长,并且希望增加超时,只需在客户机上设置它:https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.timeout(v=vs.118).aspx

也是通过查看类本身:

代码语言:javascript
复制
TimeSpan defaultTimeout = TimeSpan.FromSeconds(100.0);
TimeSpan maxTimeout = TimeSpan.FromMilliseconds((double) int.MaxValue);
TimeSpan infiniteTimeout = TimeSpan.FromMilliseconds(-1.0);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39251217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档