Microsoft KB Archive/307023

= How to make a GET request by using Visual C# =

Article ID: 307023

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q307023





For a Microsoft Visual Basic .NET version of this article, see 301102.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Net
 * System.IO



SUMMARY
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:
 * Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server
 * Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET

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 &quot;Request a Web Page&quot; 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 2005 or Visual Studio .NET. 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 = &quot;http://www.microsoft.com&quot;;   Create a new WebRequest object. You can do this only through the static Create method of the WebRequest class (&quot;New WebRequest&quot; 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); </li>  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(&quot;myproxy&quot;,80); myProxy.BypassProxyOnLocal = true;

wrGETURL.Proxy = myProxy; </li>  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, 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:

http://msdn2.microsoft.com/en-us/library/aa497287.aspx

</li>  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; </li>  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 = &quot;&quot;; int i = 0;

while (sLine!=null) {   i++; sLine = objReader.ReadLine; if (sLine!=null) Console.WriteLine(&quot;{0}:{1}&quot;,i,sLine); } Console.ReadLine; </li> 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.</li></ol>

Complete code listing
using System; using System.Net; using System.IO;

namespace MakeAGETRequest_charp {   ///     /// Summary description for Class1. ///    class Class1 {       static void Main(string[] args) {           string sURL; sURL = &quot;http://www.microsoft.com&quot;;

WebRequest wrGETURL; wrGETURL = WebRequest.Create(sURL); WebProxy myProxy = new WebProxy(&quot;myproxy&quot;,80); myProxy.BypassProxyOnLocal = true;

wrGETURL.Proxy = WebProxy.GetDefaultProxy;

Stream objStream; objStream = wrGETURL.GetResponse.GetResponseStream;

StreamReader objReader = new StreamReader(objStream);

string sLine = &quot;&quot;; int i = 0;

while (sLine!=null) {               i++; sLine = objReader.ReadLine; if (sLine!=null) Console.WriteLine(&quot;{0}:{1}&quot;,i,sLine); }           Console.ReadLine; }   } }

<div class="references_section">