0

I use the HttpClient in System.Net.Http to make requests to a web service as below:

using (var client = new HttpClient())
{
    using (var response = client.GetAsync(url).Result)
    {
        var result = response.Content.ReadAsStringAsync().Result;
    }
}

I have a sandbox application and a live application. The sandbox application has identical code (in a shared repository) which works fine, but when client.GetAsync(url).Result is called in the live application, for some reason Fiddler shows me that the requested URL has been encoded which messes the request up.

Requested URL is supposed to look like this:

/advert?paginate=1&page=1&language=en&filters[updated_at][ge]=2016-03-21%2012:19:05

But ends up looking like this:

/advert?paginate=1&page=1&language=en&filters%5Bupdated_at%5D%5Bge%5D=2016-03-21%2012:19:05

Any idea why? Thanks

N.B. Im using the Microsoft.Net.Http library from Nuget in .NET Framework 4.5

Jimbo
  • 22,379
  • 42
  • 117
  • 159
  • https://en.wikipedia.org/wiki/Percent-encoding – CSharpie Mar 21 '16 at 14:56
  • 1
    That second URL is a perfectly legitimate way to represent the first one. It sounds like, if anything, the service receiving the request has a problem with properly encoded URLs. – JLRishe Mar 21 '16 at 15:02
  • @CSharpie: I understand the point of URL encoding, but the unexpected encoding of (specifically) the square brackets is where I am confused. Specially because the same code works differently in another project. – Jimbo Mar 21 '16 at 15:02
  • @JLRishe: Unforunately, the service responds fine to the first request, but gives "no results" to the second. Which i assume is to do with the way their application parses search parameters from the URL. – Jimbo Mar 21 '16 at 15:04
  • 1
    Have you tried adding the post parameters like [this](http://stackoverflow.com/a/15176632/1789202) answer suggests? – CSharpie Mar 21 '16 at 15:15
  • @CSharpie: The web service specifically wants a GET in this case, and I dont think a `GetAsync` will accept parameters in the same way that a `PostAsync` will. I was hopeful though! – Jimbo Mar 21 '16 at 15:31
  • You really should not be doing `.Result`, you really should switch the code to async/await or use `WebClient` instead of `HttpClient` which has synchronous methods. – Scott Chamberlain Mar 21 '16 at 15:59
  • @ScottChamberlain: Appreciated. This is being used here as an example of case in point. – Jimbo Mar 22 '16 at 05:14

1 Answers1

2
  1. Please be very specific about your question:

    • you use Microsoft.Net.Http version what?
    • you compile under .NET version what?
  2. Turned out that you compile under .NET 4.0 and this is a bug I would say, because the behavior is not identical to the .NET Fx 4.5 System.Http

You can fix it by setting dontEscape to true in the Uri class:

 var url = new Uri(@"http://google.com/advert?paginate=1&page=1&language=en&filters[updated_at][ge]=2016-03-21%2012:19:05", dontEscape: true);
Serge Semenov
  • 9,232
  • 3
  • 23
  • 24