【问题标题】:Weather API http://www.weather.gov/ [closed]天气 API http://www.weather.gov/ [关闭]
【发布时间】:2015-04-02 20:21:41
【问题描述】:

我一直通过访问他们的 XML 文件从 National Weather service 获取天气信息。但是截至今天,我不断收到拒绝访问错误(403)我的服务器被阻止了吗?如果是这样,我可以使用什么来免费获取美国的天气信息?

我不敢相信我的网络服务仅仅被几次点击就被阻止了。以防这是我用来测试天气数据的计划作业:

 public override async Task ExecuteAsync()
    {
        GoogleGeocoder geocoder;
        //Geocode variables
        string apiKey = WebConfigurationManager.AppSettings["googleApiKey"];

        if (String.IsNullOrEmpty(apiKey))
        {
            geocoder = new GoogleGeocoder();
        }
        else
        {
            geocoder = new GoogleGeocoder(apiKey);
        }



        string longitude = string.Empty;

        string latitude = string.Empty;

        var xdoc = new XDocument();

        var project = Project();

        //Query for each project and get their longitude and latitude

        DataTable dataTable = SqlHelper.ExecuteDataset("getAll", "1").Tables[0];

        if (dataTable.Rows.Count > 0) {
            foreach (DataRow dataRow in dataTable.Rows) {
                Map.DataToObject(dataRow, project);

                //Find Longitude and latitude based on zipcode or address.
                IEnumerable<Address> addresses = geocoder.Geocode(project.SiteCity + "," + project.SiteState + " " + project.SitePostalCode);


                longitude = addresses.First().Coordinates.Latitude.ToString();
                latitude = addresses.First().Coordinates.Longitude.ToString();

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("http://forecast.weather.gov/MapClick.php");
                string uri = "http://forecast.weather.gov/MapClick.php?lat=" + latitude + "&lon=" + longitude + "&FcstType=dwml";

                HttpResponseMessage response = await client.GetAsync(uri);

                xdoc = XDocument.Parse(await response.Content.ReadAsStringAsync(), LoadOptions.None);


                Services.Log.Info(xdoc.Descendants("wordedForecast").Descendants("text").ElementAt(0).Value);


                //Update or create an weather entry for each project

            }
        }







        return;//Task.FromResult(true);
    }
}

【问题讨论】:

  • 我投票决定将此问题作为题外话结束,因为它质疑的是 weather.com 的服务器状态,而不是任何实际的编程问题。
  • 我不确定这是否是我的服务器代码。
  • 如果您的代码以前可以工作,现在不能工作,您的代码会怎样?不管怎样,你应该问weather.com,而不是stackoverflow。
  • 我有一个旧应用程序做了类似的事情,但我更改了很多代码。无论如何,我会关闭这个问题。我得到了我想要的。

标签: c# .net azure mobile


【解决方案1】:

您的网站似乎更改了政策。它需要设置User-Agent 标头。您只需将其设置为某个值即可。

var url = "http://forecast.weather.gov/MapClick.php?lat=42&lon=-75&FcstType=dwml";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Stackoverflow/1.0");
var xml = await client.GetStringAsync(url);

【讨论】:

  • 您先生是我的英雄。我尽我所能,但你救了我。并且相信我几乎结束了这个问题。现在可以了,非常感谢。
猜你喜欢
  • 2010-12-01
  • 1970-01-01
  • 2012-06-24
  • 2011-01-30
  • 2016-01-27
  • 2011-08-27
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多