runtime icon indicating copy to clipboard operation
runtime copied to clipboard

HttpWebRequest ignores Content-Type in Universal Windows (UWP) App

Open tzwlai opened this issue 10 years ago • 5 comments

Filing this on behalf of @AmadeusW from https://github.com/dotnet/coreclr/issues/2680:

I'm working on a UWP app that connects to a third party server and accesses their data via API. By default, they serve XML responses, and JSON when requested using Content-Type header.

Despite calling request.ContentType = "application/json"; the UWP App incorrectly makes a request without the desired Content-Type header. The same code works fine in regular windows Console Application. Furthermore, running the culprit method from a regular Win32 process (by loading the assembly via AppDomain) correctly sets Content-Type, which makes me assume that the problem lies within the UWP runtime.

Below are traces collected with Fiddler and minimal repro code. Let me know if there is a better place to file this bug than this repo.

Method Go() invoked from Win32 AppDomain, or pasted into a Win32 application:

GET http://github.com/ HTTP/1.1
Content-Type: application/json
Host: github.com
Connection: Keep-Alive

Method Go() invoked as a UWP unit test, or pasted into UWP app (non-test)

GET http://github.com/ HTTP/1.1
Host: github.com
Connection: Keep-Alive

Code to reproduce the issue:

using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using System.Net;
using System.IO;
using System.Threading.Tasks;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var x = Go().Result;
        }

        public static async Task<string> Go()
        {
            var request = WebRequest.Create($"http://github.com/");
            request.ContentType = "application/json";
            using (var response = await request.GetResponseAsync())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var test = request.Headers;
                    var data = await reader.ReadToEndAsync();
                    return data;
                } // <- set breakpoint here
            }
        }
    }
}

Observe that test contains the Content-Type header (so it didn't get "lost") Check in Fiddler that the request does not have the Content-Type header

My actual issue: I need to send & receive this:

GET http://api.translink.ca/RTTIAPI/V1/stops/61453/estimates?apiKey=XXX&routeNo=099 HTTP/1.1
Content-Type: application/json
Host: api.translink.ca
Connection: Keep-Alive


HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 1588
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 15 Jan 2016 06:04:48 GMT

[{"RouteNo":"099","RouteName":"COMMERCIAL-BROADWAY\/UBC (B-LINE)  ...

but instead the UWP app sends & receives this:

GET http://api.translink.ca/RTTIAPI/V1/stops/59997/estimates?apiKey=XXX&routeNo=099 HTTP/1.1
Host: api.translink.ca
Connection: Keep-Alive


HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 4123
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 15 Jan 2016 06:03:13 GMT

<NextBuses xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><NextBus><RouteNo>099</RouteNo><RouteName>COMMERCIAL-BROADWAY/UBC (B-LINE)  </RouteName>

tzwlai avatar Jan 20 '16 21:01 tzwlai

cc: @SidharthNabar

davidsh avatar Jan 20 '16 22:01 davidsh

I would recommend you consider using System.Net.Http APIs instead of the legacy WebRequest (System.Net.Requests contract) APIs.

davidsh avatar Jan 20 '16 22:01 davidsh

I realize that this repro works in .NET Framework (Desktop) and appears to not work with the CoreFx HTTP stack.

But the problem is more subtle. In fact, if you were to use the System.Net.Http APIs directly, you would discover that the repro doesn't work as well in .NET Framework (Desktop).

The CoreFx System.Net.Requests (contract) and WebRequest/HttpWebRequest APIs are implemented on top of the System.Net.Http (contract) HttpClient APIs.

HttpClient enforces a few things regarding request headers. In particular, the Content-Type header you are adding should really only be used when POST'ing data. According to the RFC, it is considered an entity-body header. And since you're doing a GET request, you have no entity-body. So, that request header is getting discarded.

According to standard RFC conventions, you are trying to use the Content-Type request header to influence the type of RESPONSE payload you want. However, the standard convention for that is to send a Accept request header instead.of using the Content-Type header. You can specify a request header like:

// Accept: application/json

davidsh avatar Jan 21 '16 23:01 davidsh

cc: @joshfree @stephentoub

davidsh avatar Jan 21 '16 23:01 davidsh

Awesome, thank you!

AmadeusW avatar Feb 01 '16 16:02 AmadeusW