0

我建立了谷歌地图和weather.com的混搭,每次其中一个服务器没有响应时,我的应用程序也会挂起。你认为我可以做些什么来防止或尽量减少我的网络应用程序的挂起?像你一样挂断t 导航离开该页面....我在我的应用程序代码中获得了此代码以访问天气服务;


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出现故障。我对谷歌地图没有任何问题,它的可靠性......任何人都有解决方案,如果我的页面也挂了远程服务器没有响应?如果远程服务器有响应,混搭运行顺利...

4

1 回答 1

0

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

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

于 2009-11-07T16:54:01.983 回答