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?