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

8
  • en.wikipedia.org/wiki/Percent-encoding
    – CSharpie
    Commented Mar 21, 2016 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
    Commented Mar 21, 2016 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
    Commented Mar 21, 2016 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
    Commented Mar 21, 2016 at 15:04
  • 1
    Have you tried adding the post parameters like this answer suggests?
    – CSharpie
    Commented Mar 21, 2016 at 15:15

1 Answer 1

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);
3
  • 'dontEscape' is now obsolete
    – Lance
    Commented Jun 16, 2020 at 14:14
  • @Lance can you confirm that this is not an issue with the latest .NET FX / Core? Commented Jun 16, 2020 at 15:57
  • Looks like it is still obsolete in Core 3.1 learn.microsoft.com/en-us/dotnet/api/…
    – Lance
    Commented Jul 16, 2020 at 16:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.