• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • Last Modified:

VB webrequest slow

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.
0
mbrandeemuehl
Asked:
mbrandeemuehl
  • 3
  • 2
1 Solution
 
CodeCruiserCommented:
0
 
mbrandeemuehlAuthor Commented:
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 = "https://www.ymlp.com/api/Contacts.Add?Key=F087F3VQ8SFGRBZRHAG9&Username=04s5&Email="
        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)
        allDone.WaitOne(20000)
        ' Release the WebResponse resource.
        myRequestState.response.Close()
        count2 = Now()
        count3 = DateDiff(DateInterval.Second, count1, count2)
        Dim webresponse As WebResponse = webrequest.GetResponse
        Dim datastream As Stream = webresponse.GetResponseStream

        MsgBox(count3)
        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))
            xml_reader.ReadToFollowing("Code")
            code = xml_reader.ReadElementContentAsInt
            xml_reader.ReadToFollowing("Output")
            codetext = xml_reader.ReadString
        End Using
        MsgBox(code & vbCrLf & codetext)

    End Sub



    Private Sub RespCallback(ByVal asynchronousResult As IAsyncResult)
        Try
            ' 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)
        Try
            ' 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)
            Else
                Console.WriteLine(ControlChars.Cr + "The HTML page Contents are:  ")
                If myRequestState.requestData.Length > 1 Then
                    Dim sringContent As String
                    sringContent = myRequestState.requestData.ToString()
                    Console.WriteLine(sringContent)
                End If
                Console.WriteLine(ControlChars.Cr + "Press 'Enter' key to continue........")
                responseStream.Close()
                allDone.Set()
            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

0
 
CodeCruiserCommented:
0
 
mbrandeemuehlAuthor Commented:
OK, got the original link working.  It still takes about 10-15 seconds to get a response.
0
 
mbrandeemuehlAuthor Commented:
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now