2

I want to use the Google Maps API to geocode/reverse-geocode coordinates/addresses. To do this, I use an instance of C#'s webclient class.

static string gMapsUrl= "http://maps.googleapis.com/maps/api/geocode/xml?address=  {0}&sensor=false";
public static List<Location> RetrieveCoordinate(string address)
{
    string requestUri = string.Format(gMapsUrl, address);
    string result = string.Empty;

    using (var client = new WebClient())
    {
        client.Encoding = System.Text.Encoding.UTF8;
        result = client.DownloadString(requestUri);
    }
....
}

This usually works, however lets say I want to reverse-geocode the address "Götznerstraße". If I do this manually in a browser, everything works fine, the URL of the request would be

http://maps.googleapis.com/maps/api/geocode/xml?address=götznerstraße&sensor=false

The eventual request from my program however looks like this

GET /maps/api/geocode/xml?address=G%C3%B6tznerstra%C3%9Fe&sensor=false HTTP/1.1 

This leads to Google finding no matches. To me it appears as if the webclient escapes the umlaute somewhere, which prevents me from getting results. Is there a way to stop the webclient from doing this, or make Google unescape the string again?

EDIT: I solved it, after comparing the request from the browser and my program, I realized that my browser was sending some additional headers. The changed implementation looks like this

using (var client = new WebClient())
{
    client.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    client.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
    client.Headers.Add("Accept-Language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4");
    client.Encoding = System.Text.Encoding.UTF8;
    result = client.DownloadString(requestUri);
}

Thanks a lot to everyone that helped!

  • Probably the browser is just automatically translating it back to you to make it more readable. – Uwe Keim Jan 27 '14 at 07:55
  • If you open the developer mode in your browser of choice (e.g. Ctrl+shift+I on Chrome), you'll see that the URL in the actual HTTP GET request is always percent-encoded. I would use [Fiddler](http://www.telerik.com/fiddler) and compare actual HTTP requests of a working browser and your app. – vgru Jan 27 '14 at 08:03
  • Thanks to both of you Uwe and Groo, I compared the two requests and saw that the request my program made was missing some headers! – user3239482 Jan 27 '14 at 08:11

2 Answers2

2

It has to escape the url. However there is nothing stopping you from using HttpUtility.UrlDecode to unescape it. Also have a look here for a more indepth answer.

Community
  • 1
  • 1
Alex
  • 1,110
  • 8
  • 15
0

I think the problem is actually just that Google doesn't quite know what to do with götznerstraße.

Google doesn't know götznerstraße

do you actually mean götzner straße?

Regardless, G%C3%B6tznerstra%C3%9Fe is the correct encoding of "götznerstraße", and the encoding is not causing your issue.

Mitch
  • 21,223
  • 6
  • 63
  • 86
  • Did you try entering OP's example url in the browser? Because the [unescaped version](http://maps.googleapis.com/maps/api/geocode/xml?address=götznerstraße&sensor=false) works ok on my machine. – vgru Jan 27 '14 at 07:58
  • I did, actually, it gives "INVALID_REQUEST" for me. Perhaps it is a geographic issue. – Mitch Jan 27 '14 at 08:00