【问题标题】:Catching errors or exceptions捕获错误或异常
【发布时间】:2010-12-14 02:56:28
【问题描述】:

我构建了一个 google maps 和 weather.com 的混搭,每次其中一个服务器没有响应时,我的应用程序也会挂起。您认为我可以做些什么来防止或最大限度地减少我的 Web 应用程序挂起?像挂断一样您无法离开该页面.... 我在我的应用程序代码中获得了此代码以访问天气服务;


Public Class WeatherIn
    Private _path As String
    Private _cachedFile As String
     Public Sub New(ByVal path As String)
        _path = path
        _cachedFile = String.Format("{0}\WeatherInCache.xml", _path)
    End Sub

Public Function GetWeather(ByVal arg As String) As String

    Return _getWebWeather(arg)

End Function

Private Function _getCachedWeather() As String
    Dim str As String = String.Empty


    Using reader As New StreamReader(_cachedFile)
        str = reader.ReadToEnd()
    End Using


    Return str
End Function

Private Function _getWebWeather(ByVal arg As String) As String

    Dim baseUrl As String = "http://xoap.weather.com/weather/local/{0}?cc=*&dayf=5&link=xoap&prod=xoap&par={1}&key={2}"
    Dim jane As String = arg
    Dim james As String = "api key"
    Dim john As String = "another api key"

    Dim url As String = String.Format(baseUrl, jane, james, john)


    Using client As New WebClient()

        Try

            Dim xml As New XmlTextReader(client.OpenRead(url))


            Dim xslt As New XslCompiledTransform()
            xslt.Load(_path + "/Pathto.xslt")


            Using writer As New StreamWriter(_cachedFile)
                xslt.Transform(xml, Nothing, writer)
            End Using


            Return _getCachedWeather()
        Catch exception As WebException

            Dim xmlStr As String = "<errorDoc>"
            xmlStr += "<alert>An Error Occurred!</alert>"
            xmlStr += [String].Format("<message>{0}</message>", exception.Message)
            xmlStr += "</errorDoc>"


            Dim doc As New XmlDocument()
            doc.LoadXml(xmlStr)


            Dim reader As New XmlNodeReader(doc)

            Dim xslt As New XslCompiledTransform()
            xslt.Load(_path + "/Pathto.xslt")


            Dim resultDocument As New XmlDocument()
            Using writer As XmlWriter = resultDocument.CreateNavigator().AppendChild()
                xslt.Transform(reader, DirectCast(Nothing, XsltArgumentList), writer)
            End Using

            Return resultDocument.OuterXml
        End Try
    End Using
End Function

然后我在我的页面上使用了上面的类,我这样显示天气:



'specific zip code or could be retrieved from querystring for dynamic retrieval

var jay="94576" 

Dim weather As New WeatherIn(Server.MapPath(String.Empty))
                Dim weatherData As String = weather.GetWeather(jay)


                Response.ContentType = "text/xml"
                Response.CacheControl = "no-cache"

                Response.Write(weatherData)

我通过javascript检索数据并在页面上写入。大多数时候它的weather.com出现故障。我对谷歌地图没有任何问题,它可靠....任何人都得到了我的页面挂起的解决方案如果远程服务器没有响应,mashup 也可以正常运行?如果远程服务器有响应,mashup 运行顺利...

【问题讨论】:

  • 您是如何访问这些网站的?你能显示发送请求的代码吗?

标签: asp.net google-maps weather mashup


【解决方案1】:

当依赖于外部 Web 服务时,最好异步加载,这样如果其中一个很慢,您可以向页面查看器显示某种加载微调器。如果失败,您的页面可以简单地报告从 Web 服务器读取失败并稍后重试。

在这种情况下,我会加载带有 Google 地图的页面,然后为天气数据发出 AJAX 请求。

【讨论】:

  • 是的,我在另一个页面上通过 ajax 捕获了天气数据.....其中也显示了谷歌地图
猜你喜欢
  • 2015-06-11
  • 2014-03-21
  • 2015-09-21
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 2019-09-08
相关资源
最近更新 更多