Bug 41513 - NSUrlSession does not auto follow redirects
Summary: NSUrlSession does not auto follow redirects
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.8 (tvOS / C7)
Hardware: PC Windows
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Sebastien Pouliot
Depends on:
Reported: 2016-06-05 08:34 UTC by softlion
Modified: 2016-07-20 13:17 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description softlion 2016-06-05 08:34:26 UTC
NSUrlSession should auto follow 302 calls.
Althoug HttpClient auto follow redirect is true (default), NSUrlSession does not follow 302.
Comment 2 softlion 2016-06-06 08:30:49 UTC
Well the problem is not with NSUrlSession, it is when i use HttpClient and in xamarin options i check "use NSUrlSession".

AUtoFollowHttpRedirect property is at true in my HttpClientHandler.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2016-06-06 08:37:14 UTC
OK, that's a different problem that initially stated.
Comment 4 Sebastien Pouliot 2016-06-08 17:50:56 UTC
There is no `AUtoFollowHttpRedirect` property in HttpClientHandler, so I guess you likely mean `AllowAutoRedirect` or `HttpWebRequest.AllowAutoRedirect` (which is not related to HttpClient).

This should be handled by the underlying ModernHttpClient code [1] but I'll double check from an app.

[1] https://github.com/xamarin/ModernHttpClient/blob/master/src/ModernHttpClient/iOS/NSUrlSessionHandler.cs#L417
Comment 5 Sebastien Pouliot 2016-06-09 01:58:27 UTC
Unlike the other handlers, `NSUrlSessionHandler.AllowAutoRedirect` defaults to `false`. I'll fix this so it's easier to swap handlers without code changes.

However, once set to `true`, the handler did it's expected job. If you get different results please attach a self-contained test case to the bug report and we'll look into it again.

public Task<string> Get302 (HttpMessageHandler handler)
	var client = new HttpClient (handler);
	return Task.Run (async () => await client.GetStringAsync ("https://greenbytes.de/tech/tc/httpredirects/t302loc.asis"));

public void NS302 ()
	var handler = new NSUrlSessionHandler ();
	handler.AllowAutoRedirect = true;
	var result = Get302 (handler);
	result.Wait ();
	Assert.That (result.Result, Contains.Substring ("You have reached the target"), "true");

	handler = new NSUrlSessionHandler ();
	handler.AllowAutoRedirect = false;
	result = Get302 (handler);
	try {
		result.Wait ();
	} catch (AggregateException ae) {
		var inner = ae.InnerException;
		Assert.That (inner is HttpRequestException, "HttpRequestException");
		Assert.That (inner.Message, Contains.Substring ("302 (Found)"), "302");
Comment 6 Sebastien Pouliot 2016-06-09 13:37:15 UTC
> Unlike the other handlers, `NSUrlSessionHandler.AllowAutoRedirect` defaults to `false`. 
> I'll fix this so it's easier to swap handlers without code changes.

^ now tracked in #41656
Comment 7 Vincent Dondain [MSFT] 2016-07-20 13:17:49 UTC
Sebastien seems to have addressed the issue in comment5 and bug #41656 is now fixed.

If you are still experiencing this issue please provide all the requested information 
and re-open the bug report. Thanks!