October 4, 2013 | Posted in: .Net

I’d been using the geocode SOAP service for a while and all of a sudden I started to run in to the following error:

The remote server returned an unexpected response: (417) Expectation Failed.

To get around the around the issue, I added a simple setting before making the request to the service (in bold):

 private void GeocodeAddress(string address)
 {
     try
     {
         string key = Properties.Settings.Default.BingMapsKey;
         GeocodeRequest geocodeRequest = new GeocodeRequest();

         // Set the credentials using a valid Bing Maps key
         geocodeRequest.Credentials = new GeocodeService.Credentials();
         geocodeRequest.Credentials.ApplicationId = key;

         // Set the full address query
         geocodeRequest.Query = address;

         // Set the options to only return high confidence results 
         ConfidenceFilter[] filters = new ConfidenceFilter[1];
         filters[0] = new ConfidenceFilter();
         filters[0].MinimumConfidence = GeocodeService.Confidence.High;

         // Add the filters to the options
         GeocodeOptions geocodeOptions = new GeocodeOptions();
         geocodeOptions.Filters = filters;
         geocodeRequest.Options = geocodeOptions;

         // Switch off 100 continue expectation
         System.Net.ServicePointManager.Expect100Continue = false;

         // Make the geocode request
         GeocodeServiceClient geocodeService = new GeocodeServiceClient();
         GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);

         if (geocodeResponse.Results.Length > 0)
         {
             this._addressLatitude = Convert.ToString(geocodeResponse.Results[0].Locations[0].Latitude);
             this._addressLongitude = Convert.ToString(geocodeResponse.Results[0].Locations[0].Longitude);
         }
     }
     catch
     {
         throw;
     }
 }

Be the first to comment.

Leave a Reply

*


three × 4 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>