0

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!!! :)

Rober
  • 726
  • 8
  • 27
  • Possible duplicate - http://stackoverflow.com/questions/575440/url-encoding-using-c-sharp. Try UrlEncode like this: WebRequest request = WebRequest.Create(UrlEncode(authRequest)); – smoore4 Sep 08 '14 at 09:19
  • I don't think I'm duplicating my question because I am already encoding my address parameter when I call to Uri.EscapeDataString. This method gives me the right url encoded but UrlEncode not because returns me some characters I'm not allowed to use like (, ), + Am I forced to delete the special characters from the address in order to build a proper URL???? – Rober Sep 08 '14 at 12:07
  • If it only parentheses, then you could use authRequest.Replace("(","%28") and %29 for ")" – smoore4 Sep 08 '14 at 13:33
  • that is not possible SQLDBA, because request.RequestUri.AbsoluteUri (which contains the URL to be raised) is a readonly property and you are not allowed to edit it. – Rober Sep 10 '14 at 14:41

0 Answers0