This step-by-step article describes how to make a simple
GET request to retrieve a Web page from the Internet. The Microsoft .NET
Framework includes many useful classes for networking, including the ability to
make Web requests.
Requirements
The
following list outlines the recommended hardware, software, network
infrastructure, and service packs that you need:
- Windows Server 2003, Microsoft Windows 2000
Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Microsoft
Windows NT 4.0 Server
- Microsoft Visual Studio
Note If you are behind a proxy server, you must have either an
internal Web address or static proxy values (see steps 5 and 6 of the "Request
a Web Page" section) to test the code in this article.
Request a Web page
The ability to retrieve a Web page programmatically has a many
uses. This ability was provided to Microsoft Visual Basic 6.0 programmers
through the Internet Transfer Control or through direct coding against the
WinInet 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.
By using
these objects, you can obtain a stream that represents the response for a
particular request. When you have a stream, you can read the response just as
you read from a local text file or from any other source.
To make a
GET request, follow these steps:
- Start Visual Studio.
- Create a new Console Application in Visual C#. Visual
Studio automatically creates a public class and an empty Main method.
- Verify that the project references at least System.dll.
- Use the using directive on the System namespace, the System.NET namespace, and the System.IO namespace (for the stream objects) so that you will not have to
qualify declarations from these namespaces later in your code. These statements
must be used before any other declarations.
using System;
using System.Net;
using System.IO;
- For this example, hard-code the URL as a variable. In a
real system, you would probably receive this value as a parameter to a
function, or as a command-line argument to a console application.
string sURL;
sURL = "http://www.microsoft.com";
- Create a new WebRequest object. You can do this only 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 that has this value.
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
- If you want to request URLs outside the local network, and
you are behind a proxy, you must create a WebProxy object, and then provide this object to your WebRequest object. The WebProxy object has a variety of properties, which are not set in the
sample code below, that let you to specify the same basic information that you
can set through the proxy settings in Microsoft Internet Explorer.
WebProxy myProxy = new WebProxy("myproxy",80);
myProxy.BypassProxyOnLocal = true;
wrGETURL.Proxy = myProxy; - If you want to use the settings that were already
configured in Internet Explorer, you can do this through the GetDefaultProxy static method of the WebProxy class.
wrGETURL.Proxy = WebProxy.GetDefaultProxy();
Note In Visual Studio 2005 or Visual Studio 2008, the GetDefaultProxy method works. However, this method has been deprecated. For more
information about the GetDefaultProxy method in the Microsoft .NET Framework 2.0, visit the following
Microsoft Developer Network (MSDN) Web site: - When you have completed the set up of your request by
setting the target URL and by giving any applicable proxy information, you can
use your request to obtain a Stream object that corresponds to the response for your request.
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
- When you have the response stream, you can use the stream
as you would use any other stream and you can read through the contents of the
stream line by line, or even all at the same time. The following sample code
loop reads the stream one line at a time until the ReadLine method returns null, by outputting each line to the console.
StreamReader objReader = new StreamReader(objStream);
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
Console.WriteLine("{0}:{1}",i,sLine);
}
Console.ReadLine(); - Save and then run your program. Verify that you have
configured the proxy information correctly for your environment (see steps 7
and 8). You should see lines of HTML content numbered and outputted to the
console.
Complete code listing
using System;
using System.Net;
using System.IO;
namespace MakeAGETRequest_charp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
string sURL;
sURL = "http://www.microsoft.com";
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
WebProxy myProxy = new WebProxy("myproxy",80);
myProxy.BypassProxyOnLocal = true;
wrGETURL.Proxy = WebProxy.GetDefaultProxy();
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
Console.WriteLine("{0}:{1}",i,sLine);
}
Console.ReadLine();
}
}
} For more information about Visual C# .NET, see the
following Usenet newsgroup: