Bug 22384 - HttpClient fails to handle redirects on POST requests
Summary: HttpClient fails to handle redirects on POST requests
Status: NEEDINFO
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: 2014-09-01 04:41 UTC (History)
3 users (show)

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


Attachments

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 ();
		}
	}
}

Note You need to log in before you can comment on or make changes to this bug.