I am using the GoogleMaps service to get the gps coordinates from an address provided, but if this address contains some characters like: '(', ')' or '&', then WebRequest.Create(address) method builds an Uri with those characters uncoded. I am sending the URL already encoded but looks like the Create method breaks it for some characters.
This produces an error when I execute my request. This is how the url looks like, notice the '(' inside the address parameter:
GET /maps/api/geocode/xml?address=111%CE%BF%20%CF%87%CE%BB%CE%BC%20%CE%91%CE%B3%CF%81%CE%B9%CE%BD%CE%AF%CE%BF%CF%85-%CE%91%CE%BD%CF%84%CE%B9%CF%81%CF%81%CE%AF%CE%BF%CF%85%20(%CE%BA%CF%8C%CE%BC%CE%B2%CE%BF%CF%82%20%CE%91%CE%B3.%20%CE%94%CE%B7%CE%BC%CE%B7%CF%84%CF%81%CE%AF%CE%BF%CF%85)%2C%20%CE%91%CE%B3%CF%81%CE%AF%CE%BD%CE%B9%CE%BF%2C%2030100&sensor=false&client=XXXX&signature=XXXXX&channel=XXXX HTTP/1.1
As a temporal solution I am deleting those characters but I would like to know if there is a way to build the right URL inside the WebRequest object. Here is my code sample:
private static Tuple<string, string> GetGpsCoordinates(string address)
{
string requestUri =
string.Format("/maps/api/geocode/xml?address={0}&sensor=false",
Uri.EscapeDataString(address));
// here I add some credentials for the Google API
string authRequest = GMapsAuthenticator.GetAuthorisedUrl(requestUri);
try
{
WebRequest request = WebRequest.Create(authRequest);
WebResponse response = GMapsReadThrottler.Run(() => request.GetResponse()); //error
XDocument xdoc = XDocument.Load(response.GetResponseStream());
// DO THINGS ...
}
catch (Exception)
{
throw;
}
}
Thank you guys!!! :)