httpwebrequeststreamreaderhttpwebresponsebufferedstream

Is there any way to optimize performance of reading stream?


I'm requesting remote SOAP web-service but all operation (from click search button to render interface with answer) took almost two minutes, it's too long. So I wonder if there any possible way to improve performance of the current code. Operation that parse xml and read data to database working quite well, problem only about reading answer from stream.

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

Solution

  • Here is solution!

    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