gpt4 book ai didi

c# - HttpClient.GetStringAsync(url) 抛出 "The underlying connection was closed: An unexpected error occurred on a send."Windows 8.1 C#

转载 作者:太空狗 更新时间:2023-10-29 17:49:11 26 4
gpt4 key购买 nike

我正在创建一个新的 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;
}
}

它总是进入捕获并抛出“基础连接已关闭:发送时发生意外错误。”我发现很多问题都提到了这一点,但没有解决方案对我有用,因为这是 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()

最佳答案

原来我使用的是不支持 TLS1.2 的 Windows.Net.Http。 Windows.Web.Http 确实如此。

关于c# - HttpClient.GetStringAsync(url) 抛出 "The underlying connection was closed: An unexpected error occurred on a send."Windows 8.1 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com