1

The Yahoo Weather API changed URL this week and my app fails to connect now. Please see below the code with the new URL and below in comment the old URL.

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"; }
}

I get the following error:

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]

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
  • 2
    What you're sending to the service needs to change as well. Check their API documentation to see what fields you're missing or were renamed. –  Jul 28 '17 at 16:07
  • 1
    Every time someone uses the above exception handling anti-pattern, god kills a kitten. – Uwe Keim Jul 28 '17 at 17:54
  • @Amy The API documentation is outdated and I don't know what fields I am missing or should be changed. – Peter Smeets Jul 29 '17 at 13:58
  • @UweKeim It's one of my first time using C# and I don't find a way to catch the exception or to debug in Visual Studio, I understand that might looks silly to you but a little help on how to proceed would help me more than this comment ;) – Peter Smeets Jul 29 '17 at 14:05
  • @PeterSmeets Set the VS debugger to halt on exceptions is the way to go. – Uwe Keim Jul 29 '17 at 14:43

2 Answers2

0

Try changing the URL to

string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(" + Settings.Default.WOEID.ToString() + ")%20and%20u%20%3D%20%27" + Settings.Default.Temperature + "%27&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");

EDIT:

I don't know where the documentation is but this URL.
gives

<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2017-07-31T17:50:38Z" yahoo:lang="en-US">
<results>
<channel>
<yweather:units xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" 
distance="mi" pressure="in" speed="mph" temperature="F"/>
<title>Yahoo! Weather - Seattle, WA, US</title>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2490383/
</link>
<description>Yahoo! Weather for Seattle, WA, US</description>
<language>en-us</language>
<lastBuildDate>Mon, 31 Jul 2017 10:50 AM PDT</lastBuildDate>
<ttl>60</ttl>
<yweather:location xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" city="Seattle" country="United States" region=" WA"/>
<yweather:wind xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" chill="66" direction="0" speed="14"/>
<yweather:atmosphere xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" humidity="64" pressure="1023.0" rising="0" visibility="16.1"/>
<yweather:astronomy xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" sunrise="5:45 am" sunset="8:45 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>
http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif
</url>
</image>
<item>
<title>Conditions for Seattle, WA, US at 10:00 AM PDT</title>
<geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">47.603561</geo:lat>
<geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">-122.329437</geo:long>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2490383/
</link>
<pubDate>Mon, 31 Jul 2017 10:00 AM PDT</pubDate>
<yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="32" date="Mon, 31 Jul 2017 10:00 AM PDT" temp="66" text="Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="32" date="31 Jul 2017" day="Mon" high="77" low="58" text="Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="32" date="01 Aug 2017" day="Tue" high="80" low="60" text="Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="32" date="02 Aug 2017" day="Wed" high="84" low="64" text="Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="32" date="03 Aug 2017" day="Thu" high="87" low="67" text="Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="34" date="04 Aug 2017" day="Fri" high="89" low="67" text="Mostly Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="05 Aug 2017" day="Sat" high="84" low="65" text="Partly Cloudy"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="34" date="06 Aug 2017" day="Sun" high="79" low="65" text="Mostly Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="34" date="07 Aug 2017" day="Mon" high="76" low="62" text="Mostly Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="34" date="08 Aug 2017" day="Tue" high="78" low="62" text="Mostly Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="34" date="09 Aug 2017" day="Wed" high="80" low="63" text="Mostly Sunny"/>
<description>
<![CDATA[<img src="http://l.yimg.com/a/i/us/we/52/32.gif"/> <BR /> <b>Current Conditions:</b> <BR />Sunny <BR /> <BR /> <b>Forecast:</b> <BR /> Mon - Sunny. High: 77Low: 58 <BR /> Tue - Sunny. High: 80Low: 60 <BR /> Wed - Sunny. High: 84Low: 64 <BR /> Thu - Sunny. High: 87Low: 67 <BR /> Fri - Mostly Sunny. High: 89Low: 67 <BR /> <BR /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2490383/">Full Forecast at Yahoo! Weather</a> <BR /> <BR /> (provided by <a href="http://www.weather.com" >The Weather Channel</a>) <BR /> ]]>
</description>
<guid isPermaLink="false"/>
</item>
</channel>
</results>
</query>
    <!--  total: 5  -->
WebDev90
  • 35
  • 7
0

I found the solution to update my old code and it works again. I will paste it for other people who have issues with it.

private void GetWeather()
    {
        try
        {
            String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='nederweert, lm')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
            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("query").SelectSingleNode("results").SelectSingleNode("channel");
            XmlNodeList nodes = wData.SelectNodes("/query/results/channel");

            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"; }
    }

The URL, XmlNode channel and XmlNodelist nodes have changed.