Bug 54411 - HttpClient sends ContentType application/x-www-form-urlencoded when using StreamContent
Summary: HttpClient sends ContentType application/x-www-form-urlencoded when using Str...
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2017-04-03 17:44 UTC by Oliver Weichhold
Modified: 2017-04-04 07:23 UTC (History)
1 user (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 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:
Status:
RESOLVED ANSWERED

Description Oliver Weichhold 2017-04-03 17:44:04 UTC
When sending a request like shown below the Content-Type header appears to be set to application/x-www-form-urlencoded causing a ASP.Net internal server error: InvalidDataException: Form value count limit 1024 exceeded.

using (var msg = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    RequestUri = new Uri("https://foo/bar"),
    Content = new StreamContent(contentStream)
})
{
    var handler = new AndroidClientHandler>();
    handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
    
    var httpClient = new HttpClient(handler);
    var httpResponse = await httpClient.SendAsync(msg);
}
Comment 1 Marek Habersack 2017-04-04 07:23:41 UTC
Neither AndroidHttpClient nor StreamContent set the Content-Type header. The only class in System.Net.Http that sets this header to this value is FormUrlEncodedContent (https://msdn.microsoft.com/en-us/library/system.net.http.formurlencodedcontent(v=vs.118).aspx) but since your code excerpt doesn't mention it I don't think it is involved here. The only remaining option is that the header is added by the underlying Java HTTP client. Without a full working sample to reproduce the issue (if you can, please provide a small self-contained app that reproduces the problem) it's hard to tell what's going on.
You can try to set the Content-Type header yourself by using

   var sc = new StreamContent (contentStream);
   sc.Headers.ContentType = new MediaTypeHeaderValue ("text/plain");

and see if that works around the issue.