HttpClient.GetStringAsync(url) 抛出 "The underlying connection was closed: An unexpected error occurred on a send." Windows 8.1 C#
HttpClient.GetStringAsync(url) throwing "The underlying connection was closed: An unexpected error occurred on a send." Windows 8.1 C#
我正在创建一个新的 Windows 8.1 应用程序,只是为了测试我遇到的这个错误。
该应用程序只有一个功能:
private async void TestMethod()
{
try
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("https:// url ");
}
catch (HttpRequestException e)
{
string s = e.InnerException.Message;
}
}
它总是进入捕获并抛出 "The underlying connection was closed: An unexpected error occurred on a send." 我发现很多问题都提到了这一点,但没有解决方案对我有用,因为这是一个 Windows 8.1 商店应用程序而不是 ASP.NET 应用程序。
我正在使用的 Uri 在浏览器中打开并且运行良好。
我什至看到了这篇文章:https://support.microsoft.com/en-us/kb/915599 但没有解决方案。有人遇到过这个问题吗?
编辑:
我把方法改成了async Task
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at HttpRequestTestApp.MainPage.d__0.MoveNext()
原来我使用的 Windows.Net.Http 不支持 TLS1.2。 Windows.Web.Http 确实如此。
我正在创建一个新的 Windows 8.1 应用程序,只是为了测试我遇到的这个错误。 该应用程序只有一个功能:
private async void TestMethod()
{
try
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("https:// url ");
}
catch (HttpRequestException e)
{
string s = e.InnerException.Message;
}
}
它总是进入捕获并抛出 "The underlying connection was closed: An unexpected error occurred on a send." 我发现很多问题都提到了这一点,但没有解决方案对我有用,因为这是一个 Windows 8.1 商店应用程序而不是 ASP.NET 应用程序。
我正在使用的 Uri 在浏览器中打开并且运行良好。
我什至看到了这篇文章:https://support.microsoft.com/en-us/kb/915599 但没有解决方案。有人遇到过这个问题吗?
编辑:
我把方法改成了async Task
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult) --- End of inner exception stack trace --- at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult) at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at HttpRequestTestApp.MainPage.d__0.MoveNext()
原来我使用的 Windows.Net.Http 不支持 TLS1.2。 Windows.Web.Http 确实如此。