views:

44

answers:

3

I have one URL "http://mt0.google.com/vt/lyrs=m@129&hl=en&x=11728&y=7595&z=14&s=Galileo" used to fetch Google Tiles from Internet. If I use this Google Tile URL from browser (any), I successfully get the Google Tiles (Map). But the problem is, if I try to access this URL programmaticaly, I get an Error: HTTP 403 Forbidden Error.

What could be the problem? Do I need to do some kind of setting in my program?

My code to access the URL is as follows:

byte[] imageBuffer = null;
try
{
      WebClient client = new WebClient();
      // It's a sample URL to get Tile from Google as on 29-06-2010                
      string url = "http://mt0.google.com/vt/lyrs=m@129&hl=en&x=11728&y=7595&z=14&s=Galileo";  
      imageBuffer = client.DownloadData(new Uri(url));
}
catch (WebException we)
{
  Debug.Print(we.Message);
  return null;
}
return imageBuffer;

Here, in catch block I get an Exception of HTTP 403: Forbidden Error

+1  A: 

Almost certainly, Google is analysing the User Agent string of the request, deciding that you are not a browser, meaning you are probably some form of web spider, and decides it doesn't want to waste web server time helping some other serach engine.

You can set the User Agent of your request via WebClient Headers property.

        client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
James Curran
Yes it works.. Thanks for detailed explanation. Do I need to add more appropriate "user-agent" like OS used or kind of browser installed in machine or anything else?
Shilpa Silk
+1  A: 

It is probably because you are not setting a user agent. Try adding this line of code:

client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")

See if that fixes it!

icemanind
Yes it works.. Thanks for detailed explanation. Do I need to add more appropriate "user-agent" like OS used or kind of browser installed in machine or anything else?
Shilpa Silk
You can if you want, but really it doesn't matter, if all you are trying to do is access an image.
icemanind
A: 

Accessing tiles directly is against the Google Maps Terms of Service. You should consider using the Google Static Maps API instead.

Ossama
I agree.. but I needed to use Google Tile Server only because of performance issue. Also, do you have any documentation on Google Tile Server like converting screen co-ordinates into actual Longitude, Latitude and mapping screen coordinates while zooming and panning?Thanks
Shilpa Silk
Again, let me stress the point, accessing tiles directly is against the Google Maps Terms of Service.
Ossama