1

我使用偏视图

<td style="vertical-align: top;">@Html.Action("_HavaDurumuPartial")

它现在正在服务器上工作。但有时它会出错。错误如下:

在此处输入图像描述

此错误并非总是发生。

我找不到这个问题的任何原因。我不明白为什么它有时会给出这个错误。

如果有必要,我会编写部分视图和控制器操作的内容。

行动

public ActionResult _HavaDurumuPartial(string il)
    {
        il = "Izmir";
        HttpWebRequest GoogleRequest;
        HttpWebResponse GoogleResponse = null;
        XmlDocument GoogleXMLdoc = null;
        try
        {
            GoogleRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com/ig/api?weather=" + il + "&hl=tr&ie=utf-8&oe=utf-8");
            GoogleResponse = (HttpWebResponse)GoogleRequest.GetResponse();
            GoogleXMLdoc = new XmlDocument();
            GoogleXMLdoc.Load(GoogleResponse.GetResponseStream());
            XmlNode root = GoogleXMLdoc.DocumentElement;
            XmlNodeList nodeList1 = root.SelectNodes("weather/forecast_information");
            //ViewBag.HavaDurumu = ViewBag.HavaDurumu + "<b>Şehir : " + nodeList1.Item(0).SelectSingleNode("city").Attributes["data"].InnerText + "</b>";
            XmlNodeList nodeList = root.SelectNodes("weather/current_conditions");

            ViewBag.HavaDurumu = ViewBag.HavaDurumu + "<table cellpadding=\"5\"><tbody><tr><td style=\"width:50%;\"><b><big><nobr>" + nodeList.Item(0).SelectSingleNode("temp_c").Attributes["data"].InnerText + " °C | " + nodeList.Item(0).SelectSingleNode("temp_f").Attributes["data"].InnerText + " °F</nobr></big></b></br>";
            ViewBag.HavaDurumu = ViewBag.HavaDurumu + "<b>Şuan:</b> " + nodeList.Item(0).SelectSingleNode("condition").Attributes["data"].InnerText + "";
            ViewBag.HavaDurumu = ViewBag.HavaDurumu + " " + nodeList.Item(0).SelectSingleNode("wind_condition").Attributes["data"].InnerText + "</br>" + "";
            ViewBag.HavaDurumu = ViewBag.HavaDurumu + " " + nodeList.Item(0).SelectSingleNode("humidity").Attributes["data"].InnerText;
            nodeList = root.SelectNodes("descendant::weather/forecast_conditions");
            int i = 0;
            foreach (XmlNode nod in nodeList)
            {
                if (i == 0)
                {
                    i++;
                    continue;
                }
                ViewBag.HavaDurumu = ViewBag.HavaDurumu + "</td><td align=\"center\">" + nod.SelectSingleNode("day_of_week").Attributes["data"].InnerText + "</br>" + "";
                ViewBag.HavaDurumu = ViewBag.HavaDurumu + "<img src=\"http://www.google.com" + nod.SelectSingleNode("icon").Attributes["data"].InnerText + "\" alt=\"" + nod.SelectSingleNode("condition").Attributes["data"].InnerText + "\">" + "</br>";
                ViewBag.HavaDurumu = ViewBag.HavaDurumu + nod.SelectSingleNode("low").Attributes["data"].InnerText + "°C" + "</br>";
                ViewBag.HavaDurumu = ViewBag.HavaDurumu + nod.SelectSingleNode("high").Attributes["data"].InnerText + "°C" + "</br>";
            }
            ViewBag.HavaDurumu = ViewBag.HavaDurumu + "</td></tr></tbody></table>";
        }
        catch (System.Exception ex)
        {
            ViewBag.HavaDurumu = ex.Message;
        }
        finally
        {
            GoogleResponse.Close();
        }
        return PartialView();
    }

通过此操作,我可以从 google 获取特定位置的天气。谢谢。

4

2 回答 2

2

当前对您正在使用的 Google Weather API 有间歇性的 403 Forbidden 响应。请参阅Google Weather API 403 错误

间歇性 403 响应的原因尚不清楚,但自 2012 年 8 月 7 日以来一直存在问题。

于 2012-08-09T19:35:20.723 回答
1

在您的 finally.xml 文件中添加一个空引用检查。初始化 GoogleResponse 可能会失败,因此它仍然为空。然后,您将点击 finally 块并获得空引用异常,因为当您尝试调用 .Close() 时 GoogleResponse 为空。

finally
{
    if (GoogleResponse != null)
    {
        GoogleResponse.Close();
    }
}
于 2012-08-09T19:40:50.920 回答