Googlemaps + c#

Asked

Viewed 126 times

1

I’m trying to accomplish a request in C# using the package RestSharp, for geolocation information by passing the zip code. The request this way was left:.

RestClient client = new RestClient("https://maps.googleapis.com/maps/api/geocode/json?address=" + cep + "&key=" + key);
RestRequest request = new RestRequest(Method.GET);

IRestResponse response = client.Execute(request);

But I only get TimeOut.

The strange thing is that if I run this URL in my browser, I get result.

Can someone please help me?

  • Correctly configured the key on the google developer console? Enabled the geocode API and linked it to key?

  • Yes, so when I run in the browser, the call works normally. I believe the problem is something related to C#, because I also made using angular and the same worked perfectly.

2 answers

1


Try it this way:

var client = new RestClient("https://maps.googleapis.com/");
var request = new RestRequest(Method.GET);

request.Resource = "maps/api/geocode/json?address={cep}&key={key}";
request.AddParameter("cep", cep, ParameterType.UrlSegment);
request.AddParameter("key", key, ParameterType.UrlSegment);

var response = client.Execute(request);

Another way, this time with WebRequest:

string url = $"https://maps.googleapis.com/maps/api/geocode/json?address={cep}&key={key}";

WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();

Stream data = response.GetResponseStream();
StreamReader reader = new StreamReader(data);

string responseFromServer = reader.ReadToEnd();

response.Close();
  • I tried this way, but I got the same error :/. I’m guessing it will only work in Javascript. has felt that?

  • It doesn’t make much sense, one way it should work!

  • I also think so, but the strange thing is that in angular it works and when I play directly in the browser, it also works

  • Added in the answer another alternative!

  • It really is very strange. It also did not happen this way...

  • Does the URL get badly formatted? Try getting the URL information that is being passed and confirm that it is OK.

  • Don’t forget to mark answers as useful :)

Show 3 more comments

0

The problem is that in my company the proxy was blocking calls made by applications . NET! I did the test using another network and worked normally.

Thank you all for helping me..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.