有没有办法优化读取流的性能



我正在请求远程 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

这是解决方案!

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

相关内容

  • 没有找到相关文章

最新更新