-
Notifications
You must be signed in to change notification settings - Fork 542
Redirect to relative url doesn't work with AndroidClientHandler #1923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Based on our testing, the issue seems to be caused by the handling of the redirect uri here: https://github.com/xamarin/xamarin-android/blob/master/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs#L570 Please note that according to the mentioned rfc https://tools.ietf.org/html/rfc3986#section-5 (example in section 5.4) relative uris (starting with Please see this reference source here: https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs#L110 Please also note that this issue causes real world problems with the following links not being able to load: Dropbox share links: Sharepoint online links: |
+1, seeing the same issue in our mobile app. It only started happening in our latest release, so it seems like something changed recently in the AndroidClientHandler code. |
+1, The issue appeared recently when working with services that redirects Json responses in my case. Issue seems to be related to The issue persists on the latest preview release as well. For anyone else who is experiencing the issue, changing the HttpClientHandler to Managed is a workaround. Not ideal, but a workaround until the issue is resolved. If you are using the default constructor for HttpClient, you can change the behaviour throughout: Alternatively, you may switch to Managed handler explicitly, and only when required, instead of setting it project-wide: |
@jonpryor @grendello anybody here? |
I had a similar issue recently and was abled to resolve it by using the Managed version of HttpClient and using this nuget https://github.com/tranb3r/secure-httpclient to initialize it |
I've got the exact same issue with url "https://picsum.photos/400/200/?random". |
Fixes: dotnet#1923 When a Location header contains an URL that is relative or lacks schema/host, we now use the original request URL to construct the destination URL. Additionally, if the original URL contains a fragment but the destination does not, we append the fragment to the destination.
Fixes: dotnet#1923 When a Location header contains an URL that is relative or lacks schema/host, we now use the original request URL to construct the destination URL. Additionally, if the original URL contains a fragment but the destination does not, we append the fragment to the destination.
Fixes: dotnet#1923 When a Location header contains an URL that is relative or lacks schema/host, we now use the original request URL to construct the destination URL. Additionally, if the original URL contains a fragment but the destination does not, we append the fragment to the destination.
Fixes: #1923 Consider this code: var client = new HttpClient(new AndroidClientHandler()); var res = await client.GetStringAsync("https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get"); Accessing `https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get` results in an HTTP-302 (redirect) to the location `/httpbin/get`: $ curl -D - https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get HTTP/2 302 date: Wed, 28 Nov 2018 20:52:01 GMT content-type: text/html; charset=utf-8 content-length: 0 location: /httpbin/get ... Expected behavior within Xamarin.Android is that this should work: the HTTP-302 should be followed, with `httpClient.GetStringAsync()` returning the contents of `https://nghttp2.org/httpbin/get`. What instead happens is that an exception is thrown: System.InvalidCastException: Unable to convert instance of type 'Java.Net.URLConnectionInvoker' to type 'Java.Net.HttpURLConnection' Oops. Update `AndroidClientHandler.HandleRedirect()` to improve its support for the `Location` header, such that if the `Location` header value contains a URL that is relative or lacks schema/host, we now use the original request URL to construct the destination URL. Additionally, if the original URL contains a fragment but the destination does not, we append the fragment to the destination.
Fixes: #1923 Consider this code: var client = new HttpClient(new AndroidClientHandler()); var res = await client.GetStringAsync("https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get"); Accessing `https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get` results in an HTTP-302 (redirect) to the location `/httpbin/get`: $ curl -D - https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get HTTP/2 302 date: Wed, 28 Nov 2018 20:52:01 GMT content-type: text/html; charset=utf-8 content-length: 0 location: /httpbin/get ... Expected behavior within Xamarin.Android is that this should work: the HTTP-302 should be followed, with `httpClient.GetStringAsync()` returning the contents of `https://nghttp2.org/httpbin/get`. What instead happens is that an exception is thrown: System.InvalidCastException: Unable to convert instance of type 'Java.Net.URLConnectionInvoker' to type 'Java.Net.HttpURLConnection' Oops. Update `AndroidClientHandler.HandleRedirect()` to improve its support for the `Location` header, such that if the `Location` header value contains a URL that is relative or lacks schema/host, we now use the original request URL to construct the destination URL. Additionally, if the original URL contains a fragment but the destination does not, we append the fragment to the destination.
Steps to Reproduce
Expected Behavior
The initial request returns a 302 redirect to relative url: /httpbin/get. This endpoint returns a JSON string.
Actual Behavior
Instead of following the redirect, an exception is thrown:
System.InvalidCastException: Unable to convert instance of type 'Java.Net.URLConnectionInvoker' to type 'Java.Net.HttpURLConnection'.
Please note that the code above works fine when using HttpClientHandler instead of AndroidClientHandler
Version Information
Microsoft Visual Studio Enterprise 2017
Version 15.7.4
VisualStudio.15.Release/15.7.4+27703.2035
Microsoft .NET Framework
Version 4.7.03056
Installed Version: Enterprise
Xamarin 4.10.10.2 (35a01d8dc)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.
Xamarin Designer 4.12.1 (f3257e429)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.
Xamarin.Android SDK 8.3.3.2 (HEAD/dffc59120)
Xamarin.Android Reference Assemblies and MSBuild support.
Xamarin.iOS and Xamarin.Mac SDK 11.12.0.4 (64fece5)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
The text was updated successfully, but these errors were encountered: