I have a class in a Portable Class Library (targeting .NET 4.5 and Windows Phone 8.1) that uses the HttpClient (System.Net.Http.HttpClient) class to post a string to a web service. The service requires that I set the Accept header in the request to specify API version like so:
Accept: vnd.servicehost.v1+json
I tried this in my class like this:
_httpClient.DefaultRequestHeaders.Add("Accept", "vnd.servicehost.v1+json");
And it throws a FormatException stating that my Accept header is invalid. So I try setting it this way:
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("vnd.servicehost.v1+json"));
And it still throws the FormatException. I found this answer that convinced me to try using the TryAddWithoutValidation()
method like this:
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "vnd.servicehost.v1+json");
Finally, I don't receive the FormatException! My post method works when I invoke it from within my .NET 4.5 unit/integration test project. I get back a 200 with the data I was expecting. However, when I invoke the same method from my Windows Phone 8.1 (or emulator) the service responds telling me the version I've specified in my request is invalid.
I can't figure out how to get Fiddler to show me the traffic from my Windows Phone emulator to see what the actual request looks like. My guess is the Accept header is never added. I'm hoping that a seasoned Windows Phone developer will be able to recognize some obvious mistake I'm making...