1

I'm trying to geocode using openstreetmap geocoding service from my asp.net application.

OSM geocoding service is called trhough an URL querystring. This is an example from their website:

http://nominatim.openstreetmap.org/search?q=135+pilkington+avenue,+birmingham&format=xml&polygon=1&addressdetails=1

That address returns an XML.

I'm looking for the best (best practice?) way to retrieve that XML data. I mean:

Dim MyXMLStringResponse as string
dim MyParametricURL as string

MyParametricURL="http://nominatim.openstreetmap.org/search?q=" & myaddress & "&format=xml&polygon=1&addressdetails=1"

MyXMLStringResponse = SomeObjectOrFunction.SomeMethod(MyParametricURL) 

What I'm looking for is "SomeObjectOrFunction" or "SomeMethod" to retrieve the string XML.

Just in case; nominatim.openstreetmap.org is not a webservice.

swannee
  • 3,346
  • 2
  • 24
  • 40
Leo
  • 13
  • 2
  • Be careful putting XML in the query string. You can run into length issues. See http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url – Khan Mar 06 '12 at 15:52
  • thanks Jeff, but i'm not putting XML into the query string. "myaddress" is just plain text with an address. The url returns an XML and i'm lookig the best way to call it. Regards. – Leo Mar 06 '12 at 15:55
  • Why do you aspect to receive the xml response in the querystring? In general it is an HttpResponse. You can access it with a StreamReader: `StreamReader(httpResponse.GetResponseStream()));`. See [here](http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/046b0d08-a19e-4b07-9452-f587e2903307/). – Alberto De Caro Mar 06 '12 at 15:59

1 Answers1

1

A simple way would be using the WebClient class to send the request.

This is a trimmed version of the demo code from MSDN:

        WebClient client = new WebClient ();

        // Add a user agent header in case the 
        // requested URI contains a query.

        client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

        Stream data = client.OpenRead ("http://nominatim...");
        StreamReader reader = new StreamReader (data);
        string xml = reader.ReadToEnd ();

        // "xml" now contains the response in string format
        // Do whatever (load in XmlDocument, for example)  

        // close connection  
        data.Close ();
        reader.Close ();
axel_c
  • 6,557
  • 2
  • 28
  • 41