Yahoo PlaceFinder API Connection Issues

812 Views Asked by At

I'm trying to used Yahoo's PlaceFinder API to get longitude and latitude for a postcode. Everything works fine on my local machine but when I upload it to my production server I get a connection refused error from Yahoo. I've copied my code below:

Dictionary<string, decimal> GetYahooGeoCode(string postcode)
    {

        string url = "http://where.yahooapis.com/geocode?flags=J&appid=[My_App_ID]&location=";

        decimal latitude = 0;
        decimal longitude = 0;

        try
        {
            dynamic yahooResults = new Uri(url + postcode).GetDynamicJsonObject();
            foreach (var result in yahooResults.ResultSet.Results)
            {
                latitude = (decimal)result.latitude;
                longitude = (decimal)result.longitude;
            }

            Dictionary<string, decimal> geoCode = new Dictionary<string, decimal>();

            geoCode.Add("latitude", latitude);
            geoCode.Add("longitude", longitude);

            return geoCode;
        }
        catch (Exception ex)
        {
            Log4NetLogger logger = new Log4NetLogger();

            logger.Error(ex);

            return null;
        }
    }

The error I'm getting is: No connection could be made because the target machine actively refused it. Does anyone have any ideas on this? I have done a lot of searching and can't seem to find any information on this problem. I don't know if this makes any difference but my production server is a dedicated Cloud server. Any help would be appreciated.

1

There are 1 best solutions below

4
Zoli On

I try to solve your problem here my solition:

Check your web.config:

<system.net>
    <defaultProxy />
</system.net>

And my code (GetDynamicJsonObject() not parsed the response):

        string postcode = "10003";
        string url = String.Format("http://where.yahooapis.com/geocode?state={0}&postal={1}", "NY", postcode);

        Dictionary<string, decimal> geoCode = new Dictionary<string, decimal>();

        System.Xml.Linq.XDocument xDocument = XDocument.Load(url);
        var latlon = (from r in xDocument.Descendants("Result")
                      select new { latitude = r.Element("latitude").Value, longitude = r.Element("longitude").Value }).FirstOrDefault();

        if(null != latlon)
        {
            geoCode.Add("latitude", Convert.ToDecimal(latlon.latitude));
            geoCode.Add("longitude", Convert.ToDecimal(latlon.longitude));
        }

Check place finder yahoo api parameters here http://developer.yahoo.com/geo/placefinder/guide/requests.html#base-uri