Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads
 
Add your own
alternative version

WebResourceProvider VB.NET style

, 30 Aug 2002
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

Share

About the Author

Cello
Web Developer
Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 31 Aug 2002
Article Copyright 2002 by Cello
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid