1

I have a method that looks like

public Task<HttpResponseMessage> GetLocationResponse(string url, string countryName = "The Netherlands", string cityName = "The Hague"))
{
    var httpClient = new HttpClient();
    var query = HttpUtility.ParseQueryString(string.Empty);
    query["countryName"] = HttpUtility.UrlPathEncode(countryName); // The%20Netherlands
    query["cityName"] = HttpUtility.UrlPathEncode(cityName); // The%20Hague
    var uriBuilder = new UriBuilder(url);
    uriBuilder.Query = query.ToString();
    return httpClient.GetAsync(uriBuilder.ToString());
}

What I expect is that client will make a request to

https://example.com?countryName=The%20Netherlands&cityName=The%20Hague

Instead it makes a request to

https://example.com?countryName=The%2520Netherlands&cityName=The%2520Hague

which is wrong. If I simply put the cityName and countryName directly into the query like

query["countryName"] = countryName
query["cityName"] = cityName;

I get

https://example.com?countryName=The+Netherlands&cityName=The+Hague

which again is not helpful.

The problem seems to lie with

query.ToString()

because it encodes the parameter values in a way that is not useful.

How can I get it to either not encode the parameter values, or encode them in the way that I want?

  • 3
    Does this answer your question? [A html space is showing as %2520 instead of %20](https://stackoverflow.com/questions/16084935/a-html-space-is-showing-as-2520-instead-of-20) – Ryan Wilson May 04 '21 at 12:52
  • 2
    `+` is a perfectly valid way to encode a space in the query part of a URL. So why is it a problem that this is being done? – Damien_The_Unbeliever May 04 '21 at 12:56
  • 1
    Because then the server I'm making a request to doesn't give a response. If the spaces are encoded with %20 then I do get a response –  May 04 '21 at 13:01
  • Does this answer your question? [UTF-8 URL Encode](https://stackoverflow.com/questions/62771328/utf-8-url-encode) – NineBerry May 04 '21 at 13:20
  • You can find the source code for HttpValueCollection online: https://referencesource.microsoft.com/#System.ServiceModel.Internals/System/Runtime/UrlUtility.cs,42d7707c9315ba63,references Make your own class by copying the code and adapting it as you need. – NineBerry May 04 '21 at 13:25

1 Answers1

1

When dealing with HttpClient related tasks, A better approch is to use some handly library like RestSharp.

Using RestSharp NuGet library (https://www.nuget.org/packages/RestSharp) - You don't need to worry about URL encoding, JSON parsing and a lot more

var client = new RestClient("https://countries.com");
var request = new RestRequest("GetCountry", Method.GET);
// As you mentioned, If you do no need to encode URL parameters. Add the encoding off option
request.AddQueryParameter("countryName", "India", ParameterType.QueryStringWithoutEncode);  
request.AddQueryParameter("cityName", "Kochi", ParameterType.QueryStringWithoutEncode);  
var response = client.Execute(request);

This will give

https://countries.com/GetCountry?countryName=India&cityName=Kochi

URL encoding is automaticaly handled. You don't need to worry about it

Sangeeth Nandakumar
  • 1,362
  • 1
  • 12
  • 23