GetAsync导致超时问题
本文关键字:问题 超时 GetAsync | 更新日期: 2023-09-27 18:05:10
我有下面的代码,它在获取数据时导致问题
var result = client.GetAsync(requestUri).Result;
如果我从浏览器中点击requestUri
,那么它可以正常工作。但从上面的代码来看,它给出了下面的错误。
错误:
出现一个或多个错误。发送请求时出错。基础连接已关闭:发送时发生意外错误。身份验证失败,因为远程参与方已关闭传输流。
堆栈跟踪1:
位于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(处的System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult(在System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar(上,在System.Net.TlsStream.EndWrite(IAsyncResult asyncResult(上在System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar(
堆栈跟踪2
位于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(的System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult(在System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar(上,在System.Net.TlsStream.EndWrite(IAsyncResult asyncResult(上位于System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar(位于System.Web.Http.Controllers.ApiControllerActionInvoker.d_0.MoveNext((---从引发异常的前一位置开始的堆栈结尾跟踪---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务(位于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务(位于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.TaskAwaiter
1.GetResult((位于System.Web.Http.Filters.ActionFilterAttribute.d_0.MoveNext((---从引发异常的前一位置开始的堆栈结尾跟踪---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务(位于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务(位于System.Runtime.CompilerServices.TaskAwaiter`1.GetResult((位于System.Web.Http.Filters.ActionFilterAttribute.d_5.MoveNext((
我也试过了一次失败的电话,但没有成功。在这种情况下,我们可以增加get请求的请求超时吗。
等待代码我尝试
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);
}
如果响应是长时间的,并且您想增加超时,只需在客户端上设置:https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.timeout(v=vs.118(.aspx
同样从课程本身来看:
TimeSpan defaultTimeout = TimeSpan.FromSeconds(100.0);
TimeSpan maxTimeout = TimeSpan.FromMilliseconds((double) int.MaxValue);
TimeSpan infiniteTimeout = TimeSpan.FromMilliseconds(-1.0);