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?

5
  • 3
    Does this answer your question? A html space is showing as %2520 instead of %20 Commented May 4, 2021 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? Commented May 4, 2021 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 Commented May 4, 2021 at 13:01
  • Does this answer your question? UTF-8 URL Encode Commented May 4, 2021 at 13:20
  • You can find the source code for HttpValueCollection online: referencesource.microsoft.com/#System.ServiceModel.Internals/… Make your own class by copying the code and adapting it as you need. Commented May 4, 2021 at 13:25

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

6 Comments

How does that handle spaces in the query? Will it use + or %20 to encode spaces?
In compliance to UTF-8 standard, RestSharp uses "%20" for space in URL
This discussion says something else: github.com/restsharp/RestSharp/issues/1141
There is an optional override in AddQueryParameter() function to disable encoding. IRestRequest AddQueryParameter(string name, string value, bool encode);
@NineBerry How this discussion is different my friend? "How can I get it to either not encode the parameter values, or encode them in the way that I want?" This is what the question is and we have an option to disable encoding
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.