HttpClient.GetStringAsync(url)抛出"底层连接已关闭:发送时发生意外错误".Windows 8.1 C#

yal*_*tta 9 c# exception httpclient windows-8.1

我正在创建一个新的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;
            }
        }
Run Code Online (Sandbox Code Playgroud)

它始终进入捕获并抛出"底层连接已关闭:发送时发生意外错误".我发现很多问题都提到了,但没有解决方案对我有用,因为这是Windows 8.1 Store应用程序而不是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()

yal*_*tta 9

事实证明,我使用的是不支持TLS1.2的Windows.Net.Http。Windows.Web.Http可以。

  • 通过添加以下命令来强制设置ssl协议:ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; (3认同)