Take the 2-minute tour ×

I am trying to call the geonames web services and return my result in json format. I found some tutorials on the net that use httpwebrequest however in msdn it says that this is obsolete. When my code gets to the web request it keeps timing out. Any ideas? My .asmx code is below:

 /// Summary description for Geonames
/// </summary>
[WebService(Namespace = "http://api.geonames.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Geonames : System.Web.Services.WebService
    private readonly static string FindCoordinates = "http://api.geonames.org/postalCodeSearchJSON?placename={0}&username=<username>";
    public string getCoordinates(string location)
        Uri address = new Uri(String.Format(FindCoordinates, HttpUtility.UrlPathEncode(location)));
     //   HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(address.AbsoluteUri);
     //   wr.ProtocolVersion = HttpVersion.Version10;
        string jsonResponse = string.Empty;
        WebClient client = new WebClient();
        jsonResponse = client.DownloadString(address.AbsoluteUri);

        return jsonResponse;
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Have you tried using this instead, its a lot simpler:

        WebClient client = new WebClient();

That way you can download the JSON as a string.

Also, Have you tried putting the URL


with your location into a tool like fiddler - http://www.fiddler2.com/fiddler2/?

It could be that the service is actually timing out, or the way you are building the request isnt quite right. That way you could eliminate whether it is the service or your code.

Also, you might want to remove your username from your question, just so that no-one can call the service using your username!

share|improve this answer
I tried webclient above and I still get the cannot connect to remote host error –  rbur0425 Feb 28 '11 at 19:28
It was my work network that was messing up the connection. When I was on my home network client.downloadstring works well! Thanks! –  rbur0425 Mar 1 '11 at 14:35
add comment

Your Answer


