雅虎天氣API本週更改了URL,我的應用現在無法連接。 請在下面的代碼中看到新的URL,並在下面評論舊的URL。雅虎天氣API無法連接C#
private void GetWeather()
{
try
{
string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=" + Settings.Default.WOEID.ToString() + "&u=" + Settings.Default.Temperature);
//string query = String.Format("https://weather.yahooapis.com/forecastrss?w=" + Settings.Default.WOEID.ToString() + "&u=" + Settings.Default.Temperature);
XmlDocument wData = new XmlDocument();
wData.Load(query);
XmlNamespaceManager man = new XmlNamespaceManager(wData.NameTable);
man.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("/query/results/channel/item/yweather:forecast", man);
//XmlNodeList nodes = wData.SelectNodes("/rss/channel/item/yweather:forecast", man);
Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["temp"].Value;
Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["text"].Value;
Humidity = channel.SelectSingleNode("yweather:atmosphere", man).Attributes["humidity"].Value;
WinSpeed = channel.SelectSingleNode("yweather:wind", man).Attributes["speed"].Value;
Town = channel.SelectSingleNode("yweather:location", man).Attributes["city"].Value;
TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["text"].Value;
TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["high"].Value;
TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["low"].Value;
QEvent = "connected";
}
catch { QEvent = "failed"; }
}
我得到以下錯誤:
Exception: Exception thrown: 'System.Net.WebException' in System.dll ("The remote server returned an error: (400) Bad Request."). Exception thrown: 'System.Net.WebException' in System.dll ("The remote server returned an error: (400) Bad Request.") 12.32s [11632]
您發送到服務的內容也需要更改。檢查他們的API文檔以查看您缺少或重命名的字段。 – Amy
每當有人使用上述異常處理反模式,上帝殺死一隻小貓。 –
@Amy API文檔已過時,我不知道我缺少什麼字段或應該更改。 –