9
votes

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()
1
did you try any other url endpoints? are you getting same error?Vishnu Prasad V
Post the full exception (ie use e.ToString()), not just the message of the inner exception. Also check the status code of the response. Is it a 4xx, 5xx? Use Fiddler to capture the raw request and the server response. If the server crashes and doesn't even return a full response, there's no point in changing the client code.Panagiotis Kanavos
PS How are you calling this method? async void means "fire-and-forget". It can't be awaited. Your program or test framework probably terminates before the test completes. Use async Task instead. async void should be used for event handlers or similar methods onlyPanagiotis Kanavos
please check my edited answeryalematta
Have you tried changing the url to be something like google.com, something you know should work? Also, change the return type from void to Task. That's the correct return type for async methods.Alexander Forbes-Reed

1 Answers

10
votes

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