1

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...

Community
  • 1
  • 1
Tom Faltesek
  • 2,768
  • 1
  • 19
  • 30

3 Answers3

0

You can use the Windows.Web.Http.HttpClient instead of System.Net.Http.HttpClient, i.e.:

HttpClient _httpClient = new HttpClient();
bool result = _httpClient.DefaultRequestHeaders.TryAppendWithoutValidation("Accept", "vnd.servicehost.v1+json");
var response = await _httpClient.GetAsync(uri);
kiewic
  • 15,852
  • 13
  • 78
  • 101
0

In windows phone 8 use can use WebClient for this like:

        WebClient clt = new WebClient();
        clt.Headers["Content-Type"] = "application/json";
        clt.Headers["Accept"] = "vnd.servicehost.v1+json";
        clt.UploadStringAsync(new Uri(uri, "{}");

NOTE: I am not sure about this as I haven't tested it, but you can try

Dev
  • 1,130
  • 10
  • 21
0

In my Windows Phone/Windows library I use

client.DefaultRequestHeaders.TryAddWithoutValidation()

and it works fine. So your

_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "vnd.servicehost.v1+json");

should also be ok. The only way to know for sure is to see the actual traffic from the phone to the server.

Getting Fiddler to work with the emulator is not trivial (I never accomplished it) but capturing traffic from an actual device is quite easy.

In Fiddler, in settings, you have to allow remote connections and restart Fiddler. Then in the device, make sure you are connected to the same network as your computer and in the WiFi settings set the proxy server to your_computer_ip:8888. All the trafic now should go through Fiddler and you will see, if the http requests look ok or not.

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118
  • Igor, thanks for the suggestion. I can't seem to get your proxy solution to work. I have Fiddler set up as you describe. I've tried changing the port in case there was a collision or something there. I've tried turning off the firewall on my computer. – Tom Faltesek Feb 13 '15 at 03:04