jedi nerd jedi nerd - 4 years ago 148
C# Question

clientaccesspolicy.xml Not Found 404 in Fiddler

I am working on using Google's GeoCode API to return lat/long values of an address that a user enters in a search box. My code seems to be correct, but I am getting a security exception issue. And when I use Fiddler to check the call I can see the call being sent, but there is a problem with clientaccesspolicy.xml.

It seems as though my WPF application requires Google to have a clientaccesspolicy.xml but Google doesn't have one. So I may need to proxy to make this call to Google's API. Any ideas or work arounds?

code

HttpWebRequest request = null;
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
var address = addressInput.Text;
var requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?address={0}&key=AIzaSyCgsNpuUoH7m6U7lqeZjlLZ3MgM15PW15o", Uri.EscapeDataString(address));

request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "GET";
request.BeginGetResponse(new AsyncCallback(RequestResponseHandler), null);
}

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);
}
}


Fiddler
enter image description here
enter image description here

Answer Source

As far as I understand, you use this code in a Silverlight application that is pretty picky about cross domain requests. Before perform any request Silverlight runtime tries to find clientaccesspolicy.xml on the target server (in your case maps.googleapis.com) and if it fails, it tries to find crossdomain.xml. Apparently, the server does not have such files and because of that the runtime throws SecurityException.

As a workaround you can proxy requests to Google Maps API:

  • Your Silverlight application sends a request to the server that hosts that app (and corresponding clienataccesspolicy.xml)
  • Your web server accept request and re-send it to Google API and then returns API's response to your application.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download