HTTP 403 : Forbidden error while accessing Google Tile through a C# program
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 t开发者_高级运维o 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
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!
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;)");
Accessing tiles directly is against the Google Maps Terms of Service. You should consider using the Google Static Maps API instead.
精彩评论