下面的代码会在获取数据时引发问题
var result = client.GetAsync(requestUri).Result;如果我从浏览器中点击requestUri,那么它就能正常工作。但是从上面的代码来看,它给出了以下错误。
错误:
发送基础连接关闭时发生一个或多个错误occurred.An错误:由于远程方关闭了传输流,send.Authentication上发生意外错误。
堆栈Trace1:
在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)的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](Func3 fn) )在System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)在System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
堆栈Trace2
在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult的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(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请求的请求超时吗?
我尝试过的代码
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);
}发布于 2016-08-31 14:51:29
如果响应时间长,并且希望增加超时,只需在客户机上设置它: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);https://stackoverflow.com/questions/39251217
复制相似问题