Share Pastebin
Guest
Private paste!

Untitled

By: a guest | Apr 23rd, 2010 | Syntax: VB.NET | Size: 5.14 KB | Hits: 125 | Expires: Never
Copy text to clipboard
  1. Imports System
  2. Imports System.Net
  3. Imports System.IO
  4. Imports System.Text
  5. Imports System.Threading
  6. Imports Microsoft.VisualBasic
  7.  
  8.  
  9.  
  10. Public Class RequestState
  11.     ' This class stores the state of the request
  12.     Private Shared BUFFER_SIZE As Integer = 1024
  13.     Public requestData As StringBuilder
  14.     Public bufferRead() As Byte
  15.     Public request As WebRequest
  16.     Public response As WebResponse
  17.     Public responseStream As Stream
  18.  
  19.     Public Sub New()
  20.         bufferRead = New Byte(BUFFER_SIZE) {}
  21.         requestData = New StringBuilder("")
  22.         request = Nothing
  23.         responseStream = Nothing
  24.     End Sub ' New
  25. End Class ' RequestState
  26.  
  27. Class WebRequest_BeginGetResponse
  28.     Public Shared allDone As New ManualResetEvent(False)
  29.     Private Shared BUFFER_SIZE As Integer = 1024
  30.  
  31.     Shared Sub Main()
  32.         Try
  33.             ' Create a new webrequest to the mentioned URL.
  34.             Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")
  35.             ' Create a new instance of the RequestState.
  36.             Dim myRequestState As New RequestState()
  37.             ' The 'WebRequest' object is associated to the 'RequestState' object.
  38.             myRequestState.request = myWebRequest
  39.             ' Start the Asynchronous call for response.
  40.             Dim asyncResult As IAsyncResult = CType(myWebRequest.BeginGetResponse(AddressOf RespCallback, myRequestState), IAsyncResult)
  41.             allDone.WaitOne()
  42.             ' Release the WebResponse resource.
  43.             myRequestState.response.Close()
  44.             Console.Read()
  45.         Catch e As WebException
  46.             Console.WriteLine("WebException raised!")
  47.             Console.WriteLine(ControlChars.Cr + "{0}", e.Message)
  48.             Console.WriteLine(ControlChars.Cr + "{0}", e.Status)
  49.         Catch e As Exception
  50.             Console.WriteLine("Exception raised!")
  51.             Console.WriteLine(("Source : " + e.Source))
  52.             Console.WriteLine(("Message : " + e.Message))
  53.         End Try
  54.     End Sub ' Main
  55.  
  56.     Private Shared Sub RespCallback(ByVal asynchronousResult As IAsyncResult)
  57.         Try
  58.             ' Set the State of request to asynchronous.
  59.             Dim myRequestState As RequestState = CType(asynchronousResult.AsyncState, RequestState)
  60.             Dim myWebRequest1 As WebRequest = myRequestState.request
  61.             ' End the Asynchronous response.
  62.             myRequestState.response = myWebRequest1.EndGetResponse(asynchronousResult)
  63.             ' Read the response into a 'Stream' object.
  64.             Dim responseStream As Stream = myRequestState.response.GetResponseStream()
  65.             myRequestState.responseStream = responseStream
  66.             ' Begin the reading of the contents of the HTML page and print it to the console.
  67.             Dim asynchronousResultRead As IAsyncResult = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, AddressOf ReadCallBack, myRequestState)
  68.         Catch e As WebException
  69.             Console.WriteLine("WebException raised!")
  70.             Console.WriteLine(ControlChars.Cr + "{0}", e.Message)
  71.             Console.WriteLine(ControlChars.Cr + "{0}", e.Status)
  72.         Catch e As Exception
  73.             Console.WriteLine("Exception raised!")
  74.             Console.WriteLine(("Source : " + e.Source))
  75.             Console.WriteLine(("Message : " + e.Message))
  76.         End Try
  77.     End Sub ' RespCallback
  78.     Private Shared Sub ReadCallBack(ByVal asyncResult As IAsyncResult)
  79.         Try
  80.             ' Result state is set to AsyncState.
  81.             Dim myRequestState As RequestState = CType(asyncResult.AsyncState, RequestState)
  82.             Dim responseStream As Stream = myRequestState.responseStream
  83.             Dim read As Integer = responseStream.EndRead(asyncResult)
  84.             ' Read the contents of the HTML page and then print to the console.
  85.             If read > 0 Then
  86.                 myRequestState.requestData.Append(Encoding.ASCII.GetString(myRequestState.bufferRead, 0, read))
  87.                 Dim asynchronousResult As IAsyncResult = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, AddressOf ReadCallBack, myRequestState)
  88.             Else
  89.                 Console.WriteLine(ControlChars.Cr + "The HTML page Contents are:  ")
  90.                 If myRequestState.requestData.Length > 1 Then
  91.                     Dim sringContent As String
  92.                     sringContent = myRequestState.requestData.ToString()
  93.                     Console.WriteLine(sringContent)
  94.                 End If
  95.                 Console.WriteLine(ControlChars.Cr + "Press 'Enter' key to continue........")
  96.                 responseStream.Close()
  97.                 allDone.Set()
  98.             End If
  99.         Catch e As WebException
  100.             Console.WriteLine("WebException raised!")
  101.             Console.WriteLine(ControlChars.Cr + "{0}", e.Message)
  102.             Console.WriteLine(ControlChars.Cr + "{0}", e.Status)
  103.         Catch e As Exception
  104.             Console.WriteLine("Exception raised!")
  105.             Console.WriteLine("Source :{0} ", e.Source)
  106.             Console.WriteLine("Message :{0} ", e.Message)
  107.         End Try
  108.     End Sub ' ReadCallBack
  109.  
  110. End Class ' WebRequest_BeginGetResponse