【问题标题】:Is there any way to optimize performance of reading stream?有没有办法优化读取流的性能?
【发布时间】:2013-09-14 09:18:50
【问题描述】:

我正在请求远程 SOAP 网络服务,但所有操作(从单击搜索按钮到呈现带有答案的界面)都花了将近两分钟,太长了。所以我想知道是否有任何可能的方法来提高当前代码的性能。 解析xml并将数据读取到数据库的操作运行良好,问题仅在于从流中读取答案。

Public Shared Function CallWebService(ByVal an As String, ByVal xmlcommand As String) As String
        Dim _url = "http://testapi.interface-xml.com/appservices/ws/FrontendService"
        Dim soapEnvelopeXml As XmlDocument = CreateSoapEnvelope(xmlcommand)
        Dim webRequest As HttpWebRequest = CreateWebRequest(_url, an)
        webRequest.Proxy = System.Net.WebRequest.DefaultWebProxy
        InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest)
        Dim asyncResult As IAsyncResult = webRequest.BeginGetResponse(Nothing, Nothing)
        asyncResult.AsyncWaitHandle.WaitOne()
        Dim soapResult As String
        Using webResponse As WebResponse = webRequest.EndGetResponse(asyncResult)
            Using bs As New BufferedStream(webResponse.GetResponseStream())
                Using rd As New StreamReader(bs)
                    soapResult = rd.ReadLine()
                    Return soapResult
                End Using
            End Using
        End Using
    End Function

【问题讨论】:

    标签: httpwebrequest streamreader httpwebresponse bufferedstream


    【解决方案1】:

    这里有解决方案!

    Public Shared Function CallWebService(ByVal an As String, ByVal xmlcommand As String) As String
            Dim _url = "http://testapi.interface-xml.com/appservices/ws/FrontendService"
            Dim soapEnvelopeXml As XmlDocument = CreateSoapEnvelope(xmlcommand)
            Dim webRequest As HttpWebRequest = CreateWebRequest(_url, an)
            webRequest.Proxy = System.Net.WebRequest.DefaultWebProxy
            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate")
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest)
            Dim asyncResult As IAsyncResult = webRequest.BeginGetResponse(Nothing, Nothing)
            asyncResult.AsyncWaitHandle.WaitOne()
            Dim soapResult As String
            Using webResponse As WebResponse = webRequest.EndGetResponse(asyncResult)
                Using bs As New BufferedStream(webResponse.GetResponseStream())
                    Using gz As New GZipStream(bs, CompressionMode.Decompress)
                        Using rd As New StreamReader(gz)
                            soapResult = rd.ReadLine()
                            Return soapResult
                        End Using
                    End Using
                End Using
            End Using
        End Function
    

    【讨论】:

    • GZipStream 就是答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    相关资源
    最近更新 更多