Request and Response in Visual Basic
Submitted by Yorkiebar on Sunday, May 18, 2014 - 21:04.
Introduction:
This is a simple tutorial on how to use the System.Net namespace in Visual Basic to send a request and receive a response from a server.
Imports:
First we need to import the namespace. This enables us to use the required functions and classes...
Request:
To send a request we first create an object of HTTPWebRequest...
We then set that object equal to a new HTTPWebRequest with the function of '.Create()' while parsing it the server URL, in this case, Bing...
Response:
Next we create an object to hold the response. This object is the type of HTTPWebResponse...
Next we set the response object to the request's response...
Done!:
From now on we can use that response to do things such as get the source of the response (web page from the request server)...
Full Source:
Here is the full source file...
- Imports System.Net
- Dim req As HttpWebRequest
- req = HttpWebRequest.Create("http://www.bing.com")
- Dim res As HttpWebResponse
- res = req.GetResponse()
- Using reader As New System.IO.StreamReader(res.GetResponseStream())
- MsgBox(reader.ReadToEnd()) 'Source
- End Using
- Imports System.Net
- Class Form1
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Dim req As HttpWebRequest
- req = HttpWebRequest.Create("http://www.bing.com")
- Dim res As HttpWebResponse
- res = req.GetResponse()
- Using reader As New System.IO.StreamReader(res.GetResponseStream())
- MsgBox(reader.ReadToEnd()) 'Source
- End Using
- End Sub
- End Class
Add new comment
- 446 views