Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm creating a new Windows 8.1 App just for testing this error I am getting. The app has only one function:

private async void TestMethod()
        {
            try
            {                       
                HttpClient httpClient = new HttpClient();
                HttpResponseMessage response = await httpClient.GetAsync("https:// url ");                    
            }
            catch (HttpRequestException e)
            {
                string s = e.InnerException.Message;
            }
        }

It is always entering the catch and throwing "The underlying connection was closed: An unexpected error occurred on a send." I found a lot of questions mentioning that but no solution worked for me since this is a Windows 8.1 Store App not an ASP.NET app.

The Uri I am using opens in the Browser and is functioning very well.

I even got to this article: https://support.microsoft.com/en-us/kb/915599 but no solution. Anyone has encoutered this problem before?

Edit:

I changed the method to use 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()
like image 804
yalematta Avatar asked Jun 08 '16 13:06

yalematta


1 Answers

It turns out I was using Windows.Net.Http that doesn't support TLS1.2. Windows.Web.Http does.

like image 105
yalematta Avatar answered Nov 14 '22 22:11

yalematta