Bug 22384 - HttpClient fails to handle redirects on POST requests
Summary: HttpClient fails to handle redirects on POST requests
Status: RESOLVED NORESPONSE
Alias: None
Product: Class Libraries
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-08-26 20:30 UTC by praburaj@gmail.com
Modified: 2018-03-13 11:07 UTC (History)
3 users (show)

Tags:
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 GitHub or Developer Community 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:
Status:
RESOLVED NORESPONSE

Description praburaj@gmail.com 2014-08-26 20:30:07 UTC
For example: 

A POST action results in a 302, I notice the HttpClient fails to do the redirects. 

For example: 
var formParameters = new List<KeyValuePair<string, string>>
{
   new KeyValuePair<string, string>("Key1", "Value1"),
};

var content = new FormUrlEncodedContent(formParameters.ToArray());
response = httpClient.PostAsync("Account/LogOff", content).Result;
responseContent = response.Content.ReadAsStringAsync().Result;

//If the above form post triggers a 302, HttpClient fails to redirect or change the state of the CookieContainer. I see this as a difference in behavior from .net. 

I'm from asp.net vnext team. I ended up skipping this scenario while doing some automation. 

https://github.com/aspnet/MusicStore/blob/dev/test/E2ETests/SmokeTests.cs#L345
Comment 1 Marek Safar 2014-09-01 04:41:35 UTC
I cannot reproduce this behaviour with master or Mono 3.10 using following repro.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;

class X
{
	const int WaitTimeout = 5000;

	static string port, TestHost, LocalServer;

	static HttpListener CreateListener (Action<HttpListenerContext> contextAssert)
	{
		var l = new HttpListener ();
		l.Prefixes.Add (string.Format ("http://+:{0}/", port));
		l.Start ();
		l.BeginGetContext (ar => {
			var ctx = l.EndGetContext (ar);

			try {
				if (contextAssert != null)
					contextAssert (ctx);
			} finally {
				ctx.Response.Close ();
			}
		}, null);

		return l;
	}

	static void Main ()
	{
		port = "810";
		TestHost = "localhost:" + port;
		LocalServer = string.Format ("http://{0}/", TestHost);

		var listener = CreateListener (l => {
			var request = l.Request;
			var response = l.Response;

			response.StatusCode = (int) HttpStatusCode.Moved;
			response.RedirectLocation = "http://xamarin.com/";
		});

		try {
			var formParameters = new List<KeyValuePair<string, string>>
			{
			   new KeyValuePair<string, string>("Key1", "Value1"),
			};

			var chandler = new HttpClientHandler ();
			var httpClient = new HttpClient (chandler);

			var content = new FormUrlEncodedContent (formParameters.ToArray ());
			var response = httpClient.PostAsync ("http://localhost:810", content).Result;
			var responseContent = response.Content.ReadAsStringAsync ().Result;

			return;

		} finally {
			listener.Abort ();
			listener.Close ();
		}
	}
}
Comment 2 Marek Safar 2018-03-13 11:07:39 UTC
We have not received the requested information. If you are still experiencing this issue please provide all the requested information and reopen the bug report.

Thank you!