Anda di halaman 1dari 2


The Microsoft .NET Framework includes many useful classes for networking, including the
ability to make Web requests. This article demonstrates how to make a simple GET request to
retrieve a Web page from the Internet.

The following list outlines the recommended hardware, software, network infrastructure, and
service packs that you will need:
Microsoft Windows 2000 Professional, Microsoft Windows Server 2003, Windows 2000 Server,
Windows 2000 Advanced Server, or Windows NT 4.0 Server
Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
NOTE: If you are behind a proxy server, you will need either an internal Web address or static
proxy values (see steps 7 and 8) to test the code in this article.

Request a Web Page

The ability to retrieve a Web page programmatically has a wide variety of uses. This ability was
provided to Microsoft Visual Basic 6.0 programmers through the Internet Transfer Control or
through direct coding against the WinInet application programming interfaces (APIs). In .NET,
the System.Net namespaces provide the WebRequest class to encapsulate a request for an
Internet resource, and the WebResponse class to represent the data that is returned. Using
these objects, you can obtain a stream that represents the response for a particular request.
After you have a stream, reading the response is no different than reading from a local text file
or other source.

To make a GET request, follow these steps:

Open Visual Studio .NET or Visual Studio 2005
Create a new Console Application in Microsoft Visual Basic .NET or in Microsoft Visual Basic
2005. Visual Basic .NET or Visual Basic 2005 creates a Module for you, along with an empty
Main() procedure.
Make sure that at least the System namespace is referenced by the project.
Use the Imports statement on the System, System.NET, and System.IO (for the stream objects)
namespaces so that you will not be required to qualify declarations from these namespaces
later in your code. These statements must be used prior to any other declarations.
Imports System
Imports System.Net
Imports System.IO
For this example, just hard-code the uniform resource locator (URL) into a variable. In a real
system, you would likely receive this value as a parameter to a function or perhaps as a
command-line argument to a console application.
Dim sURL As String
sURL = ""
Create a new WebRequest object, which can only be done through the static Create method of
the WebRequest class ("New WebRequest" is not valid). Supply the target URL as part of the
call to Create to initialize the object with this value.
Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
If you are behind a proxy, you need to create a WebProxy object and provide it to your
WebRequest object if you wish to request URLs outside of your local network. The WebProxy
object has a variety of properties (which are not being set in this example code) that allow you
to specify the same basic information as can be set through the proxy settings of Microsoft
Internet Explorer.
Dim myProxy As New WebProxy("myproxy", 80)
myProxy.BypassProxyOnLocal = True

wrGETURL.Proxy = myProxy
If you wish to just use whatever settings have been configured in Internet Explorer, you can do
that through the GetDefaultProxy static method of the WebProxy class.
wrGETURL.Proxy = WebProxy.GetDefaultProxy()
Now that your request is set up with the target URL and any applicable proxy information, you
can use it to obtain a Stream object corresponding to the response to your request.
Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()
After you have the response stream, you can treat it like any other stream (such as from
opening a text file) and read through its contents line by line or even all at once. The loop below
reads the stream one line at a time until the ReadLine method returns Nothing, outputting each
line to the console.
Dim objReader As New StreamReader(objStream)
Dim sLine As String = ""
Dim i As Integer = 0

Do While Not sLine Is Nothing

i += 1
sLine = objReader.ReadLine
If Not sLine Is Nothing Then
Console.WriteLine("{0}:{1}", i, sLine)
End If
Save and then run your program, making sure that you have configured the proxy information
(steps 7 and 8) correctly for your environment. You should see lines of HTML content numbered
and outputted to the console.