Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / Visual Basic

WebResourceProvider VB.NET style

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
30 Aug 20023 min read 82.5K   263   24  
Ravi Bhavnani's WebResourceProvider ported to the .NET Framework
I just strugged with this recently as well. If you are not sending
parameters, then you set the ContentLength to 0. In that case, using
WebClient is easier. With parameters, I've found the UploadParameters method
of WebClient to be the simplest. Here's some sample code. The first two are
different implementions for a POST with no parameters. The third sets
parameters.

Imports System.Net
Imports System.IO

Private Sub submitPost_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles submitPost.Click
If httpPostAddress.Text <> "" Then
Try
Dim postRequest As WebRequest
Dim postResponse As WebResponse
Dim responseStream As Stream
Dim responseReader As StreamReader
Dim responseText As String

postRequest = WebRequest.Create(httpPostAddress.Text)
With postRequest
.ContentLength = 0
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
End With
postResponse = postRequest.GetResponse()
responseStream = postResponse.GetResponseStream()
responseReader = New StreamReader(responseStream)
responseText = responseReader.ReadToEnd
postReturnValue.Text = responseText
responseReader.Close()
responseStream.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
End Try
End If
End Sub

Private Sub submitPostWebClient_Click(ByVal sender As System.Object, ByVal
e As _
System.EventArgs) Handles submitPostWebClient.Click

If httpPostAddress.Text <> "" Then
Dim webClientID As WebClient = New WebClient()
Dim responseStream As Stream = webClientID.OpenRead(httpPostAddress.Text)
Dim responseReader As New StreamReader(responseStream)
Dim responseText As String = responseReader.ReadToEnd

postReturnValue.Text = responseText
responseStream.Close()
responseReader.Close()
End If
End Sub

Private Sub submitPostParameters_Click(ByVal sender As System.Object, ByVal
e As _
System.EventArgs) Handles submitPostParameters.Click

If emailAddress.Text <> "" Then
Dim webAddress As String = urlWithParameters.Text
Dim webClientID As WebClient = New WebClient()
Dim parameterTable As New
System.Collections.Specialized.NameValueCollection()

With parameterTable
.Add("email", emailAddress.Text)
.Add("name", nameBox.Text)
.Add("location", locationBox.Text)
End With

Dim responseArray As Byte() = webClientID.UploadValues(webAddress,
"POST", _
parameterTable)
postReturnValue.Text =
System.Text.Encoding.ASCII.GetString(responseArray)
End If
End Sub

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions