VB webrequest slow

Posted on 2011-10-27
Last Modified: 2012-08-13
I'm using the following code to communicate with a site:

Dim webrequest As System.Net.WebRequest = System.Net.WebRequest.Create(strhttp)
        webrequest.Proxy = System.Net.WebRequest.DefaultWebProxy
        Dim webresponse As System.Net.WebResponse = webrequest.GetResponse()
        Dim datastream As System.IO.Stream = webresponse.GetResponseStream
        Dim reader As New System.IO.StreamReader(datastream)
        Dim responsefromserver As String = reader.ReadToEnd()

When I run the URL through Chrome or Firefox (Windows 7), the return from the remote website is < 1 sec.  When I run this code, it takes 12 - 14 seconds for the GetResponse line to return.  I also see that it takes 12 - 14 seconds for IE to return a response.  

I've searched the forums and tried everything I saw there:
1. set max connections to 10 in config file
2. Set proxy to Nothing
3.  Close webresponse

It seems this has something to do with how IE is handling communications, but I haven't been able to track it down.
Question by:mbrandeemuehl
    LVL 83

    Expert Comment


    Author Comment

    Tried the attached code  and the code hangs on the AllDone.Waitone() line.  (Also struggling to figure out where the output goes).  Very possible that I have not set up the code properly - I'm new to the Visual Studio environment, mostly been VBA before.
    Imports System
    Imports System.Net
    Imports System.IO
    Imports System.Text
    Imports System.Threading
    Imports Microsoft.VisualBasic
    Module Module1
        Public Class RequestState
            ' This class stores the state of the request
            Private Shared BUFFER_SIZE As Integer = 1024
            Public requestData As StringBuilder
            Public bufferRead() As Byte
            Public request As WebRequest
            Public response As WebResponse
            Public responseStream As Stream
        End Class
        Public allDone As New ManualResetEvent(False)
        Private BUFFER_SIZE As Integer = 1024
        Sub AddContact(ByVal email As String, ByVal GroupID As Integer)
            Dim strhttp1 As String
            Dim strhttp2 As String
            Dim strhttp4 As String
            Dim code As Integer
            Dim codetext As String
            Dim count1 As Date
            Dim count2 As Date
            Dim count3 As Integer
            strhttp1 = ""
            strhttp2 = "&GroupID="
            strhttp4 = strhttp1 & email & strhttp2 & GroupID
            Dim webrequest As System.Net.WebRequest = System.Net.WebRequest.Create(strhttp4)
            webrequest.Proxy = System.Net.WebRequest.DefaultWebProxy
            count1 = Now()
            'Dim webresponse As System.Net.WebResponse = webrequest.GetResponse()
            Dim myRequestState As New RequestState()
            ' The 'WebRequest' object is associated to the 'RequestState' object.
            myRequestState.request = webrequest
            ' Start the Asynchronous call for response.
            Dim asyncResult As IAsyncResult = CType(webrequest.BeginGetResponse(AddressOf RespCallback, myRequestState), IAsyncResult)
            ' Release the WebResponse resource.
            count2 = Now()
            count3 = DateDiff(DateInterval.Second, count1, count2)
            Dim webresponse As WebResponse = webrequest.GetResponse
            Dim datastream As Stream = webresponse.GetResponseStream
            Dim reader As New StreamReader(datastream)
            Dim responsefromserver As String = reader.ReadToEnd()
            Using xml_reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(New System.IO.StringReader(responsefromserver))
                code = xml_reader.ReadElementContentAsInt
                codetext = xml_reader.ReadString
            End Using
            MsgBox(code & vbCrLf & codetext)
        End Sub
        Private Sub RespCallback(ByVal asynchronousResult As IAsyncResult)
                ' Set the State of request to asynchronous.
                Dim myRequestState As RequestState = CType(asynchronousResult.AsyncState, RequestState)
                Dim myWebRequest1 As WebRequest = myRequestState.request
                ' End the Asynchronous response.
                myRequestState.response = myWebRequest1.EndGetResponse(asynchronousResult)
                ' Read the response into a 'Stream' object.
                Dim responseStream As Stream = myRequestState.response.GetResponseStream()
                myRequestState.responseStream = responseStream
                ' Begin the reading of the contents of the HTML page and print it to the console.
                Dim asynchronousResultRead As IAsyncResult = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, AddressOf ReadCallBack, myRequestState)
            Catch e As WebException
                Console.WriteLine("WebException raised!")
                Console.WriteLine(ControlChars.Cr + "{0}", e.Message)
                Console.WriteLine(ControlChars.Cr + "{0}", e.Status)
            Catch e As Exception
                Console.WriteLine("Exception raised!")
                Console.WriteLine(("Source : " + e.Source))
                Console.WriteLine(("Message : " + e.Message))
            End Try
        End Sub ' RespCallback
        Private Sub ReadCallBack(ByVal asyncResult As IAsyncResult)
                ' Result state is set to AsyncState.
                Dim myRequestState As RequestState = CType(asyncResult.AsyncState, RequestState)
                Dim responseStream As Stream = myRequestState.responseStream
                Dim read As Integer = responseStream.EndRead(asyncResult)
                ' Read the contents of the HTML page and then print to the console.
                If read > 0 Then
                    myRequestState.requestData.Append(Encoding.ASCII.GetString(myRequestState.bufferRead, 0, read))
                    Dim asynchronousResult As IAsyncResult = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, AddressOf ReadCallBack, myRequestState)
                    Console.WriteLine(ControlChars.Cr + "The HTML page Contents are:  ")
                    If myRequestState.requestData.Length > 1 Then
                        Dim sringContent As String
                        sringContent = myRequestState.requestData.ToString()
                    End If
                    Console.WriteLine(ControlChars.Cr + "Press 'Enter' key to continue........")
                End If
            Catch e As WebException
                Console.WriteLine("WebException raised!")
                Console.WriteLine(ControlChars.Cr + "{0}", e.Message)
                Console.WriteLine(ControlChars.Cr + "{0}", e.Status)
            Catch e As Exception
                Console.WriteLine("Exception raised!")
                Console.WriteLine("Source :{0} ", e.Source)
                Console.WriteLine("Message :{0} ", e.Message)
            End Try
        End Sub ' ReadCallBack 
    End Module

    Open in new window

    LVL 83

    Accepted Solution


    Author Comment

    OK, got the original link working.  It still takes about 10-15 seconds to get a response.

    Author Comment

    I've traced this down and it appears that the server that I'm accessing is very variable in performance and what I've been seeing with the VB app is just the way it performs.  Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now