Async HTTP POST/GET in VB .Net 2005 !!

Hi,

I am a VB6 developer and I am learning VB 2005, I wanted to know how can I make HTTP POST AND GET requests in asynchronous mode, I wanted to use it to download file from HTTP servers Simultaneously (probably using multi-threading).

Thanks.



Answer this question

Async HTTP POST/GET in VB .Net 2005 !!

  • DBAMANI

    If all you want to do is download a file asynchronously, please see the System.Net.WebClient class. This class abstracts much of the complexity and is even simpler to use the HttpWebRequest class noted above for such a scenario.

  • PatrickCairns

    This is what I came up with..


    I was also having the same problem, for my problem I had made this works for me. (I am learning VB 2005, so dont expect too much)

    Public Sub PostData(ByVal strURL As String, Optional ByVal POST1 As String = "", Optional ByVal POST2 As String = "")
    Try
    ' Declare a variable named client of type WebClient.
    Dim myWebClient As WebClient
    ' Instantiate a WebClient object and assign it to the 'client' variable.
    myWebClient = New WebClient

    ' Add handler to process theWebClient's DownloadProgressChanged events.
    AddHandler myWebClient.UploadProgressChanged, AddressOf DownloadProgress

    ' Add handler to process theWebClient's DownloadCompleted event.
    AddHandler myWebClient.UploadStringCompleted, AddressOf DownloadCompleted

    ' Declare a variable named downloadUri of type Uri
    Dim downloadUri As Uri
    ' Instantiate a new Uri object and assign it to the 'downloadUri' variable.
    downloadUri = New Uri(strURL)

    'myWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4")
    'myWebClient.Headers.Add("Accept-Language", "en-us,en;q=0.5")


    If txtProxy.Text.Length > 0 Then
    Dim proxy As New WebProxy(txtProxy.Text, True)
    myWebClient.Proxy = proxy
    End If


    '' For Intial Posting Of Data
    If strURI.Length > 0 Then
    myWebClient.UploadStringAsync(downloadUri, "filename=" & POST1 )
    End If



    Catch webex As WebException
    Console.WriteLine(webex.Message)
    End Try
    End Sub

    Public Sub DownloadProgress(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs)
    Debug.Print(e.ProgressPercentage)

    End Sub

    Public Sub DownloadCompleted(ByVal sender As Object, ByVal e As UploadStringCompletedEventArgs)
    Msgbox "Hurray!"
    End Sub



  • Salman Maredia

    Thanks all for reply, I guess this will help me http://msdn2.microsoft.com/en-us/library/ms144246.aspx , will post after testing


  • CNB

    Check

    http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx.

    It is more efficient to use async IO then threads. Obviuosly threads are still in the picture, but the Begin methods do async IO under the cover and this is optimal.


  • Tomys

    I tried the suggestions but failed to get any result, Pls. post a code sample.

    What I wanted to achieve is POST filecode value to the server which will return the file as response to my POST, posted below is the response captured from server.

    HTTP/1.1 200 OK
    Date: Thu, 06 Jul 2006 05:27:08 GMT
    Connection: close
    Content-Type: application/octet-stream
    Accept-Ranges: bytes
    Content-Disposition: Attachment; filename=tk.txt
    Content-Length: 1125

    FILE CONTENT GOES HERE.................................................. ....................................................................



  • kshill

    Deepak,

    You dont have to use asynchronous mode for such a feature, use threading you mentioned.

    Create a single component that can download a file, use a couple of threads to download the files.



  • Async HTTP POST/GET in VB .Net 2005 !!