Google Weather API 403错误
我决定从Google的Weather API中提取信息 – 我在下面使用的代码工作正常。
XmlDocument widge = new XmlDocument(); widge.Load("https://www.google.com/ig/api?weather=Brisbane/dET7zIp38kGFSFJeOpWUZS3-"); var weathlist = widge.GetElementsByTagName("current_conditions"); foreach (XmlNode node in weathlist) { City.Text = ("Brisbane"); CurCond.Text = (node.SelectSingleNode("condition").Attributes["data"].Value); Wimage.ImageUrl = ("http://www.google.com/" + node.SelectSingleNode("icon").Attributes["data"].Value); Temp.Text = (node.SelectSingleNode("temp_c").Attributes["data"].Value + "°C"); } }
正如我所说,我能够从XML文件中提取所需的数据并显示它,但是如果刷新页面或当前会话仍处于活动状态,我会收到以下错误:
WebException未被用户代码处理 – 远程服务器返回错误:403 Forbidden Exception。
我想知道这是否与访问该特定XML文件的某种访问限制有关?
进一步研究和改编建议
如下所述,这绝不是最佳实践,但我已经包含了我现在用于例外的捕获。 我在Page_Load上运行此代码,所以我只是回到页面。 从那以后我没有发现任何问题。 性能方面我并不过分担心 – 我没有注意到任何负载时间的增加,这个解决方案是暂时的,因为这一切都是出于测试目的。 我还在使用Yahoo的Weather API。
try { XmlDocument widge = new XmlDocument(); widge.Load("https://www.google.com/ig/api?weather=Brisbane/dET7zIp38kGFSFJeOpWUZS3-"); var list2 = widge.GetElementsByTagName("current_conditions"); foreach (XmlNode node in list2) { City.Text = ("Brisbane"); CurCond.Text = (node.SelectSingleNode("condition").Attributes["data"].Value); Wimage.ImageUrl = ("http://www.google.com/" + node.SelectSingleNode("icon").Attributes["data"].Value); Temp.Text = (node.SelectSingleNode("temp_c").Attributes["data"].Value + "°C"); } } catch (WebException exp) { if (exp.Status == WebExceptionStatus.ProtocolError && exp.Response != null) { var webres = (HttpWebResponse)exp.Response; if (webres.StatusCode == HttpStatusCode.Forbidden) { Response.Redirect(ithwidgedev.aspx); } } }
谷歌文章说明APIerror handling
Google API处理错误
谢谢:
https://stackoverflow.com/a/12011819/1302173(Catch 403并召回)
https://stackoverflow.com/a/11883388/1302173 (error handling和一般Google API信息)
https://stackoverflow.com/a/12000806/1302173 (响应处理/ json缓存 – 未来计划)
替代
我最近发现了这个很好的开源替代品
OpenWeatherMap – 免费天气数据和预报API
这与服务的更改/中断有关。 请参阅: http : //status-dashboard.com/32226/47728
我已经使用谷歌的Weather API一年多来为手机服务器供电,以便PolyCom手机接收天气页面。 它运行错误超过一年。 截至2012年8月7日,经常发生间歇性403错误。
我每小时打一次服务(一如既往)所以我认为请求的频率不是问题。 更有可能的是403的间歇性与部分推出配置更改或Google的CDN更改有关。
Google Weather API实际上不是已发布的API。 这是一项明显设计用于iGoogle的内部服务,因此支持程度不确定。 我昨天在Twitter上发了推文并没有收到任何回复。
切换到推广天气API可能会更好,例如: WUnderground Weather或Yahoo Weather 。
我昨天自己添加了以下’除非已定义’error handlingperl代码以应对此问题,但如果问题仍然存在,我将切换到更完全支持的服务:
my $url = "http://www.google.com/ig/api?weather=" . $ZipCode ; my $tpp = XML::TreePP->new(); my $tree = $tpp->parsehttp( GET => $url ); my $city = $tree->{xml_api_reply}->{weather}->{forecast_information}->{city}->{"-data"}; unless (defined($city)) { print "The weather service is currently unavailable. \n"; open (MYFILE, '>/home/swarmp/public_html/status/polyweather.xhtml'); print MYFILE qq(\n); print MYFILE qq(\n); print MYFILE qq(\n); print MYFILE qq(Weather is Unavailable! \n); print MYFILE qq(\n); print MYFILE qq(\n); print MYFILE qq(The weather service is currently unavailable from the data vendor.\n); print MYFILE qq(
\n); print MYFILE qq(\n); print MYFILE qq(\n); close MYFILE; exit(0); }...
这绝不是最佳做法,但我在一些WP7和Metro应用程序中大量使用此API。 我通过捕获exception(大部分时间为403)并简单地重新调用catch内部的服务来处理这个问题,如果Google端部出现错误,通常会短暂地执行,只会产生1或2个额外的调用。
那是我们发现的相同的事情。
比较错误请求和工作请求中的请求标头。 工作请求包括cookie。 但他们来自哪里?
从谷歌删除所有浏览器cookie。 天气API呼叫将不再适用于您的浏览器。 浏览到google.com然后再到天气api,它将再次运行。
Google会检查Cookie以阻止多个API呼叫。 在处理所有天气api请求之前获取cookie一次将解决问题。 Cookie将在一年后到期。 我假设您将更频繁地重新启动您的应用程序,然后每年一次。 这样你就会得到一个新的。 获取每个请求的cookie将导致同样的问题:太多不同的请求。
一个提示:天气不经常变化,因此缓存json信息(可能一小时)。 这将减少耗时的操作作为请求!
我发现如果您在干净的浏览器中尝试请求(例如Chrome上的新窗口隐身模式),Google天气服务会正常运行。 cookies的可能问题?
- entity framework4 TPHinheritance,如何将一种类型转换为另一种?
- WindowsIdentity和Classic .Net应用程序池
- 从TableAdapter获取@@ IDENTITY
- http.context.user和thread.currentprincipal之间的区别以及何时使用它们?
- 收到“无法找到类型或命名空间名称’LayoutsPageBase’的错误”
- 创建为.Net Auth Cookie交换自定义令牌的Owin Auth Provider
- 从C#和Dreamweaver开始使用ASP.net?
- 在asp.net中异步运行方法
- AutoCompleteExtender正在触发,webservice正在返回结果,但这些结果未显示