0

我正在尝试使用 Yahoo 的 PlaceFinder API 来获取邮政编码的经度和纬度。在我的本地机器上一切正常,但是当我将它上传到我的生产服务器时,我收到来自 Yahoo 的连接被拒绝错误。我在下面复制了我的代码:

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;
        }
    }

我得到的错误是:无法建立连接,因为目标机器主动拒绝了它。有人对此有任何想法吗?我做了很多搜索,似乎找不到有关此问题的任何信息。我不知道这是否有任何区别,但我的生产服务器是专用的云服务器。任何帮助,将不胜感激。

4

1 回答 1

0

我尝试在这里解决您的问题:

检查您的 web.config:

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

我的代码(GetDynamicJsonObject() 没有解析响应):

        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));
        }

在此处检查地点查找器 yahoo api 参数http://developer.yahoo.com/geo/placefinder/guide/requests.html#base-uri

于 2012-03-01T17:15:11.477 回答