【发布时间】:2017-01-08 03:20:15
【问题描述】:
我有以下代码在获取数据时导致问题
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](Func3 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(Func3 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.TaskAwaiter1.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()
我还尝试了等待呼叫,但没有运气。在这种情况下我们可以增加获取请求的请求超时时间吗?
我试过的等待代码
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);
}
【问题讨论】:
-
你有一些内在的例外吗?那里可能有更多信息
-
不要使用
.Result,要么让你的调用堆栈异步并使用await,要么从HttpClient切换到WebClient并使用它的同步方法。 -
@ScottChamberlain :是的,我也试过了。
-
然后显示该代码而不是使用
.Result方法。 -
不使用堆栈溢出的
quote特性,将异常的文本放在代码块中,这样更容易阅读。还可以尝试使用copy exception detail to clipboard 按钮,然后将该文本完整地粘贴到代码块中。
标签: c# asp.net-mvc asp.net-web-api async-await