mbrandeemuehl
asked on
VB webrequest slow
I'm using the following code to communicate with a site:
Dim webrequest As System.Net.WebRequest = System.Net.WebRequest.Crea te(strhttp )
webrequest.Proxy = System.Net.WebRequest.Defa ultWebProx y
Dim webresponse As System.Net.WebResponse = webrequest.GetResponse()
Dim datastream As System.IO.Stream = webresponse.GetResponseStr eam
Dim reader As New System.IO.StreamReader(dat astream)
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.
Dim webrequest As System.Net.WebRequest = System.Net.WebRequest.Crea
webrequest.Proxy = System.Net.WebRequest.Defa
Dim webresponse As System.Net.WebResponse = webrequest.GetResponse()
Dim datastream As System.IO.Stream = webresponse.GetResponseStr
Dim reader As New System.IO.StreamReader(dat
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.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK, got the original link working. It still takes about 10-15 seconds to get a response.
ASKER
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.
http://msdn.microsoft.com/en-us/library/system.net.webrequest.begingetresponse.aspx