Google API WebRequest not working

299 Views Asked by At

My request isn't working correctly. I have a search box with a search button. Users can enter an address in the search box and then the address is sent out Google's API. But this isn't working for some reason. Does anyone know why? I will also include a pic of what the value of request is when debugging.

private void RequestResponseHandler(IAsyncResult asyncResult)
{
    HttpWebResponse response = null;
    Stream responseStream = null;
    try
    {
        response = (HttpWebResponse)request.EndGetResponse(asyncResult);
        responseStream = response.GetResponseStream();
        var xdoc = XDocument.Load(responseStream);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

enter image description here

1

There are 1 best solutions below

11
On BEST ANSWER

The geocode API returns JSON. Example: https://maps.googleapis.com/maps/api/geocode/json?address=fifth%20avenue&key=AIzaSyCgsNpuUoH7m6U7lqeZjlLZ3MgM15PW15o

But here you are trying to put this JSON into an XML document:

var xdoc = XDocument.Load(responseStream);

I would recommend you using a JSON parser instead.

Now that you have shown the actual exception it seems that in the environment in which you are working (Silverlight?) you don't have permission to access cross domain resources:

enter image description here

You are getting a Security Error because of this. Depending on the actual environment you are working with there might be different ways to resolve this issue. For example in Silverlight you might need to include a clientaccesspolicy.xml file in order to make cross domain calls. In this file you need to explicitly allow the domains to which you need to make HTTP requests (in your case that would be maps.googleapis.com).