Open divil5000 opened 3 years ago
@divil5000 do you use AndroidHttpClientHandler
with System.Net.Http.HttpClient
or just the standard System.Net.Http.HttpClient
without any replacement handlers?
In our app we are explicitly passing it:
httpClient = new HttpClient(new Xamarin.Android.Net.AndroidClientHandler());
@divil5000 thanks! I will need to see the entire stack trace or, preferably, it would be great if you could create and attach a simple app that uses exactly the code that fails for you.
I believe I gave you everything you needed to reproduce this in my initial message. However, I have done the work for you as requested. Here's some code:
var httpClient = new HttpClient(new Xamarin.Android.Net.AndroidClientHandler());
try
{
var result = await httpClient.GetAsync("https://localhost:12345");
}
catch (HttpRequestException)
{
// Should get here
}
catch (WebException ex)
{
// Gets here instead
}
And here's the stack trace:
{System.Net.WebException: Failed to connect to localhost/127.0.0.1:12345 ---> Java.Net.ConnectException: Failed to connect to localhost/127.0.0.1:12345
at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue args) [0x0006e] in <24e422c426e0468ca1fd74b59870ff08>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue parameters) [0x00014] in <24e422c426e0468ca1fd74b59870ff08>:0
at Javax.Net.Ssl.HttpsURLConnectionInvoker.Connect () [0x00000] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-29/mcw/Javax.Net.Ssl.HttpsURLConnection.cs:433
at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass44_0.
at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x000e4] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:406 --- End of inner exception stack trace --- at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java.Net.HttpURLConnection httpConnection, System.Threading.CancellationToken cancellationToken, Xamarin.Android.Net.AndroidClientHandler+RequestRedirectionState redirectState) [0x0016e] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:413 at Xamarin.Android.Net.AndroidClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x00286] in /Users/builder/azdo/_work/278/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:287 at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:506 at App2.MainActivity.FabOnClick (System.Object sender, System.EventArgs eventArgs) [0x0003e] in C:\Users\None\source\repos\App2\MainActivity.cs:53 }
Note: If you attempt to access an internet endpoint with no connection, an Java.Net.UnknownHostException is thrown. The methods on HttpClient should not throw that exception.
@grendello as this recently reappeared in https://github.com/dotnet/runtime/issues/99568. Would it be possible to schedule this for .NET 10?
@simonrozsival definitely fine for .NET 10
Steps to Reproduce
Use the Android HttpClient implementation.
Call HttpClient.SendAsync or probably any of its other methods, on an endpoint that does not exist such as http://localhost:12345.
Expected Behavior
The documentation for HttpClient.SendAsync says it throws HttpRequestException in the event of a problem connecting to a server. It certainly should not throw WebException, which is a legacy exception.
Actual Behavior
WebException is thrown.
Version Information
Log File
Not needed.