0

我正在请求远程 SOAP Web 服务,但所有操作(从单击搜索按钮到呈现带有答案的界面)都花了将近两分钟,太长了。所以我想知道是否有任何可能的方法来提高当前代码的性能。解析 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
4

1 回答 1

0

这是解决方案!

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
于 2013-09-17T07:04:40.807 回答