Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Based on code on p. 255 of Wrox's Pro Windows 8 Programming[http://p2p.wrox.com/book-professional-windows-8-programming-application-development-c-xaml-703/], I am trying to call virtualearth's REST API like so:

C#
async internal static Task<CivicAddress> GetCivicAddress(Location location)
{
    HttpClient http = new HttpClient();
    StringBuilder sb = new StringBuilder("http://deb/virtualearth.net/REST/v1/Locations/");
    sb.Append(location.Latitude.ToString());
    sb.Append(",");
    sb.Append(location.Longitude.ToString());
    sb.Append("?o=xml&key=");
    sb.Append(App.BingMapsKey);

    HttpResponseMessage resp = await http.GetAsync(sb.ToString());
    resp.EnsureSuccessStatusCode();
    Stream strom = await resp.Content.ReadAsStreamAsync();

    XDocument xdoc = XDocument.Load(strom);

    XNamespace bmn = App.BingMapsNamespace;
    String addressLine = (from l in xdoc.Descendants(bmn + "AddressLine") select l).First().Value;
    String municipality = (from l in xdoc.Descendants(bmn + "Locality") select l).First().Value;
	. . .


...which (sb.ToString()) ends up being (if the latitude is 44 and the longitude is -122):

HTML
http://dev/virtualearth.net/REST/v1/Locations/44,-122?o=xml&key=[my bing maps key]


However, the code is bombing out, and never getting past the "*HttpResponseMessage resp = await http.GetAsync(sb.ToString());*" line.

I wondered if the URL was bogus, and checked the book's site for errate, but found nothing. I entered the URL directly into a browser. It tells me, **ERR_NAME_NOT_RESOLVED**

I added a catch block to the code:

C#
catch (HttpRequestException hre)
{
    Debug.WriteLine("Exception in GetCivicAddress(): {0} Inner Ex = {1}, Stack Trace = {2}",
        hre.Message, hre.InnerException.ToString(), hre.StackTrace);
}


...and get, "Exception in GetCivicAddress(): An error occurred while sending the request.
Inner Ex = System.Net.WebException: The remote name could not be resolved: 'dev'
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar),
Stack Trace = at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Photrax.PhotraxUtils.<getcivicaddress>d__f.MoveNext()
A first chance exception of type 'System.NullReferenceException' occurred in Photrax.exe


Has virtualearth been hit by a mass injection of Corona, or what is going on here?
Posted
Comments
BillWoodruff 25-Oct-14 1:50am    
Why not do some research and see if the Virtual Earth rest service has changed its access permissions, or calling-form ?

URL seems to be wrong.

Check - Location via Point[^]
 
Share this answer
 
One of those links provided by a commenter led me to another link here , from which I ended up wit this, which works:

C#
HttpClient http = new HttpClient();
String httpqry = String.Format("http://dev.virtualearth.net/REST/v1/Locations/{0},{1}?o=xml&key={2}", location.Latitude.ToString(), location.Longitude.ToString(), App.BingMapsKey);

HttpResponseMessage resp = await http.GetAsync(httpqry);
resp.EnsureSuccessStatusCode();

Stream strom = await resp.Content.ReadAsStreamAsync();

XDocument xdoc = XDocument.Load(strom);

XNamespace bmn = App.BingMapsNamespace;
String addressLine = (from l in xdoc.Descendants(bmn + "Address") select l).First().Value;
String municipality = (from l in xdoc.Descendants(bmn + "Locality") select l).First().Value;
String stateProv = (from l in xdoc.Descendants(bmn + "AdminDistrict") select l).First().Value;
String postalCode = (from l in xdoc.Descendants(bmn + "PostalCode") select l).First().Value;
String country = (from l in xdoc.Descendants(bmn + "CountryRegion") select l).First().Value;


How the new URL differs from the old, I can't tell - maybe it's in how it's built, or...? At any rate, it works, so I'm happy and will move on to the next challenge.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900