0
votes

I have C# code written and suddenly it is throwing an error. But the strange thing is, it works sometimes but also throws an error sometimes:

The remote server returned an error: (403) Forbidden.

Please see the code if you see any issues:

C#

char[] delimiterChars = { ',' };
XmlDocument xmldoc = new XmlDocument();
string url = "https://maps.googleapis.com/maps/api/geocode/xml?address=Richmond&sensor=false";
xmldoc.Load(url); // I get the error on this line.
XmlNode LattNode = 
xmldoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat");
XmlNode LongNode = xmldoc.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng");

// if condition here to check if LattNode and LongNode.InnerText is not empty

longitude = Convert.ToDouble(LongNode.InnerText);
lattitude = Convert.ToDouble(LattNode.InnerText);

return true;

Please see the error's screenshot here: https://snag.gy/u3QhaV.jpg

When you type this URL "https://maps.googleapis.com/maps/api/geocode/xml?address=Richmond&sensor=false" Then you will always get the result, but for some reason, through the code, it doesn't work sometimes.

Please help.

1
Possible duplicate of XmlDocument opening from URL - mjwills
If you do two Load in a row (xmldoc and xmldoc1) the 2nd always fails. The server is checking two make sure the same user isn't connecting multiple times to prevent service attacks. - jdweng
I think this is google blocking or limiting your calls. Are you following their Terms of use for their API? - Richard Hubley
Keys are now required for Google's services. - geocodezip
@jdweng it is 1 load at a time. I have a location search where users type the location and hit enter and then the above code runs to get information about the google map. sometimes it works but some times it doesnt. - orbnexus

1 Answers

0
votes

If you Connect a Internet that has real / dedicated ip address than it will work fine , And if you already working on real / dedicated ip address than you can put your code on try catch so that if errors occurs your system wont stop.

try{ your code; return true;}
catch(exception e){
return false;
}