}
catch (Exception error) when (!(error is OperationCanceledException))
{
- throw CreateWrappedException(error, cancellationToken);
+ throw CreateWrappedException(error, host, port, cancellationToken);
}
finally
{
catch (Exception e)
{
socket.Dispose();
- throw CreateWrappedException(e, cancellationToken);
+ throw CreateWrappedException(e, host, port, cancellationToken);
}
return new NetworkStream(socket, ownsSocket: true);
if (lastException != null)
{
- throw CreateWrappedException(lastException, cancellationToken);
+ throw CreateWrappedException(lastException, host, port, cancellationToken);
}
// TODO: find correct exception to throw here.
throw new HttpRequestException("No host found.");
}
- private static Exception CreateWrappedException(Exception error, CancellationToken cancellationToken)
+ private static Exception CreateWrappedException(Exception error, string host, int port, CancellationToken cancellationToken)
{
return CancellationHelper.ShouldWrapInOperationCanceledException(error, cancellationToken) ?
CancellationHelper.CreateOperationCanceledException(error, cancellationToken) :
- new HttpRequestException(error.Message, error, RequestRetryType.RetryOnNextProxy);
+ new HttpRequestException($"{error.Message} ({host}:{port})", error, RequestRetryType.RetryOnNextProxy);
}
}
}
using System.Diagnostics;
using System.IO;
using System.Linq;
+using System.Net.Sockets;
using System.Net.Test.Common;
using System.Text;
using System.Threading;
}
[Fact]
+ [OuterLoop("Failing connection attempts take long on windows")]
+ public async Task GetContentAsync_WhenCanNotConnect_ExceptionContainsHostInfo()
+ {
+ using Socket portReserver = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ portReserver.Bind(new IPEndPoint(IPAddress.Loopback, 0));
+ IPEndPoint ep = (IPEndPoint)portReserver.LocalEndPoint;
+
+ using var client = CreateHttpClient();
+
+ HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetStreamAsync($"http://localhost:{ep.Port}"));
+ Assert.Contains($"localhost:{ep.Port}", ex.Message);
+ }
+
+ [Fact]
public async Task GetContentAsync_NullResponse_Throws()
{
using (var client = new HttpClient(new CustomResponseHandler((r,c) => Task.FromResult<HttpResponseMessage>(null))))