12

Weather.gov 当前观察提要突然开始对来自 HTTPClient 的所有请求失败,同样我观察到互联网上许多使用 AJAX 调用 weather.gov 的网站也失败了。

所有调用weather.gov 当前观察提要的结果,例如http://w1.weather.gov/xml/current_obs/TAPA.xml,返回403。所述URL 在浏览器中正确解析。

4

5 回答 5

21

联系weather.gov得到了非常快速的响应,即:

访问weather.gov 资源的应用程序现在需要在任何HTTP 请求中提供一个User-Agent 标头。没有用户代理的请求会被自动阻止。由于少数客户使用的资源远远超过大多数人认为合理的资源,因此我们实施了此使用政策。

我们建议提供以下格式的用户代理字符串:

应用程序名称/vX.Y ( http://your.app.url/ ;contact.email@example.com)

这将唯一标识您的应用程序,并允许我们在我们观察到可能导致阻塞的异常应用程序行为时与您联系并与您合作。

如果您在验证您的应用程序发送正确的标头后仍有问题,请随时给我们发电子邮件。

感谢您使用weather.gov。

=======

这是一段 C# 代码。关键是您需要创建请求对象,然后在进行调用之前将自定义用户代理字符串附加到它。

...
var request = new HttpRequestMessage(HttpMethod.Post, httpClient.BaseAddress.AbsoluteUri);
request.Headers.Add("User-Agent", "MyApplication/v1.0 (http://foo.bar.baz; foo@bar.baz)");
var httpResponse = httpClient.SendAsync(request).Result;
...

希望这对人们有所帮助。干杯

于 2015-09-17T22:40:15.667 回答
2

这很好,但是当您使用 XMLDocument 并像这样调用 Load() 时,您不能设置“User-Agent”(这曾经有效):

        XmlDocument doc = new XmlDocument();
        string stationName = @"http://w1.weather.gov/xml/current_obs/PASC.xml";
        doc.Load(stationName);  // exception 403 occurs here now

相反,现在您需要执行 GET,然后将 User-Agent 设置为您的公司或电子邮件,然后使用 XmlDocument,如:

       XmlDocument doc = new XmlDocument();

       string stationName = @"http://w1.weather.gov/xml/current_obs/PASC.xml";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(stationName);

        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "MyApplication/v1.0 (http://foo.bar.baz; foo@bar.baz)";
        request.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream resStream = response.GetResponseStream();

        doc.Load(resStream);
        try
        {

            XmlNodeList list = doc.GetElementsByTagName("temp_f");
            if (list.Count > 0)
            {
                float fTemperature = float.Parse(list.Item(0).InnerText);

            }
        }
于 2015-10-02T01:15:40.820 回答
2

如果你从Études for Elixir的Étude 12-1中找到这个,这就是最终对我有用的东西

  s_url = "http://w1.weather.gov/xml/current_obs/" <> weather_station <> ".xml"
 :httpc.request(:get, { to_char_list(s_url),
                        [ {'User-Agent','ErlangEtudes/v1.0 (http://example.org;me@example.org)' } ] 
                      },[],[] )
于 2016-01-23T03:05:14.660 回答
1

我有同样的问题,但使用的是 PowerShell。这是一种使用 HttpWebRequest 设置用户代理的方法。

$weather_url = "http://w1.weather.gov/xml/current_obs/KLNK.xml"
$request = [System.Net.HttpWebRequest]::Create($weather_url)
$request.UserAgent = " {Enter your agent} "
$response = $request.GetResponse()

$doc = New-Object System.Xml.XmlDocument    
$doc.Load($response.GetResponseStream())

$temp_f = [int] $doc.current_observation.temp_f
于 2015-10-29T19:47:25.780 回答
-1

这个答案帮助了我,但实际上没有用。我仍然收到错误响应。

我在这一行将 Post 更改为 Get:

var request = new HttpRequestMessage(HttpMethod.Post...

至:

var request = new HttpRequestMessage(HttpMethod.Get...

然后,它工作得很好。

于 2021-11-01T21:02:15.157 回答