mit*_*mus 9 c# xml asp.net google-api
我决定从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");
}
}
Run Code Online (Sandbox Code Playgroud)
正如我所说,我能够从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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
/sf/answers/840827361/(Catch 403并召回)
/sf/answers/831837191/(错误处理和一般Google API信息)
/sf/answers/840056451/(响应处理/ json缓存 - 未来计划)
我最近发现了这个很好的开源替代品
Cle*_*ndo 12
这与服务的更改/中断有关.请参阅: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.
我昨天自己添加了以下'除非已定义'错误处理perl代码以应对此问题,但如果问题仍然存在,我将切换到更完全支持的服务:
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(<?xml version="1.0" encoding="utf-8"?>\n);
print MYFILE qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">\n);
print MYFILE qq(<html xmlns="http://www.w3.org/1999/xhtml">\n);
print MYFILE qq(<head><title>Weather is Unavailable!</title></head>\n);
print MYFILE qq(<body>\n);
print MYFILE qq(<p>\n);
print MYFILE qq(The weather service is currently unavailable from the data vendor.\n);
print MYFILE qq(</p>\n);
print MYFILE qq(</body>\n);
print MYFILE qq(</html>\n);
close MYFILE;
exit(0);
}...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8984 次 |
| 最近记录: |