Bug 34715 - HttpClient incorrectly works with multiple headers
Summary: HttpClient incorrectly works with multiple headers
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2015-10-09 09:57 UTC by Pavel Martynov
Modified: 2016-09-13 14:14 UTC (History)
2 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 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:

Description Pavel Martynov 2015-10-09 09:57:20 UTC
Server response:
$ curl -XGET -I http://test-host:8082/
HTTP/1.1 200 OK
Server: Suave/ (http://suave.io)
Date: Fri, 09 Oct 2015 13:53:36 GMT
X-Custom-Header: first
X-Custom-Header: second
Content-Type: text/html
Content-Length: 6

As you can see there is two "X-Custom-Header" headers.

Client code:
var client = new System.Net.Http.HttpClient();
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, new Uri("http://test-host:8082/"));
var resp = client.SendAsync(request).Result;
foreach (var header in resp.Headers)
	Console.WriteLine("{0}: {1}", header.Key, string.Join(", ", header.Value));

Mono result:
$ mono Test.exe
Server: Suave/, (http://suave.io)
Date: Fri, 09 Oct 2015 13:37:58 GMT
X-Custom-Header: second

Only one "X-Custom-Header: second"

Bu under .NET 4.0:
X-Custom-Header: first, second
Date: Fri, 09 Oct 2015 12:51:58 GMT
Server: Suave/, (http://suave.io)

Both X-Custom-Header: first, second

Version: mono-complete
Comment 1 Marek Safar 2015-10-12 11:08:49 UTC
This is actually WebRequest bug

Simple repro

using System;
using System.Threading;
using System.IO;
using NUnit.Framework;
using System.Net;
using System.Collections.Generic;
using System.Net.Http.Headers;

class X
	static string port = "8810";

	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;

	public static void Main ()
		var listener = CreateListener (l => {
			var request = l.Request;
			var response = l.Response;
			response.Headers.Add ("X-Custom-Header", "A");
			response.Headers.Add ("X-Custom-Header", "B");

		try {
			var TestHost = "localhost:" + port;
			var LocalServer = string.Format ("http://{0}/", TestHost);

			HttpWebRequest wr = HttpWebRequest.CreateHttp (LocalServer);
			var resp = wr.GetResponse ();
			var vls = resp.Headers.GetValues ("X-Custom-Header");

			// Print 1 where 2 is expected
			Console.WriteLine (vls.Length);
		} finally {
			listener.Close ();
Comment 2 Marek Safar 2016-09-13 14:14:57 UTC
Fixed in master and Mono 4.8