Microsoft KB Archive/815657

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

Article ID: 815657

Article Last Modified on 1/11/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-







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



For a Microsoft Visual C# .NET version of this article, see 307023.

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

IN THIS TASK

 * SUMMARY
 * Requirements
 * Request a Web Page
 * Complete Code Listing
 * REFERENCES



SUMMARY
The Microsoft .NET Framework includes many classes for networking. You can also use these classes to make Web requests. This article describes how to make a GET request to retrieve a Web page.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

Note If you are behind a proxy server, you must have an internal Web address or static proxy values to test the code in this article. For more information, see steps 7 and 8 in the &quot;Request a Web Page&quot; section.

back to the top

Request a Web Page
In Microsoft Visual Basic 6.0, you can use the Internet Transfer Control control or you can use the direct coding against the WinInet application programming interfaces (APIs) to retrieve a Web page programmatically.

In the NET Framework, the System::Net namespaces provide the WebRequest class to encapsulate a request for an Internet resource. The System::Net namespaces also provide the WebResponse class to represent the returned data.

If you use these classes, you can receive a stream that represents the response for a particular request. When you have this stream, you can read the response as you would read from a local text file or from any other source.

To make a GET request, follow these steps:  Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project. In Visual Studio .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.

In Visual Studio .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.

In Visual C++ 2005, click Visual C++ under Project Types, and then click CLR Console Application under Templates.

Visual C++ .NET creates a default file and a __tmain function. Visual C++ 2005 creates a default file and a main function.  Verify that the project references at least the System.dll assembly.

In Visual C++ .NET 2002, explicitly refer System.dll at the top of the source file. To do this, use following code: For additional information about how to add references to a Managed Visual C++ project in Visual Studio .NET 2002 and in Visual Studio .NET 2003, click the following article number to view the article in the Microsoft Knowledge Base:
 * 1) using 
 * 2) include 
 * 3) using  // Refers &quot;System.Dll&quot; file.

310674 HOW TO: Add References to a Managed Visual C++ Project

</li>  Use the using directive on the System namespace, on the System::NET namespace, and on the System::IO namespace for the stream objects so that you do not have to qualify declarations from these namespaces later in your code. You must use the following statements before you use any other declarations: using namespace System; using namespace System::Net; using namespace System::IO; </li>  For this example, declare a string variable, and then hard-code the URL to this string variable. In a real system, you may receive the following value as a parameter to a function, or as a command-line argument to a console application: String *sURL = S&quot;http://www.microsoft.com&quot;; </li>  Create a new WebRequest object. Do not use the following code to do this: new WebRequest Instead, you must use the static Create method of the WebRequest class. Supply the target URL as part of the call to the Create method to initialize the object with the following value: WebRequest *wrGETURL; wrGETURL = WebRequest::Create(sURL); </li>  If you want to request URLs outside your local network and you are behind a proxy server, you must create a WebProxy object and then provide this object to your WebRequest object, as follows: WebProxy *myProxy = new WebProxy(S&quot;myproxy&quot;, 80); myProxy->BypassProxyOnLocal = true;

wrGETURL->Proxy = myProxy; Note The WebProxy object has a variety of properties that permit you to set the same basic information that you can set by using the proxy settings in Microsoft Internet Explorer. These properties are not set in this sample code. </li>  To use the already configured Internet Explorer settings, use the GetDefaultProxy static method of the WebProxy class, as follows: wrGETURL->Proxy = WebProxy::GetDefaultProxy; </li>  When your request is set up with the target URL and any applicable proxy information, you can use your request to receive a Stream object that corresponds to the response to your request as follows: Stream *objStream = wrGETURL->GetResponse->GetResponseStream; </li>  When you have the response stream, you can use this stream as you would use any other stream -- for example, as you would use the steam that occurs when you open a text file. Also, you can read through the contents of the stream line-by-line or all at the same time. The following sample code loop-reads the stream one line at a time until the ReadLine method returns a NULL result by outputting each line to the console: while(sLine != NULL) {   i += 1; sLine = objReader->ReadLine; if(sLine != NULL) Console::WriteLine(S&quot;{0}:{1}&quot;, __box(i), sLine); } </li> Save and then run your program. Verify that you have configured the proxy information correctly for your environment (see steps 7 and 8). Lines of HTML content are numbered and are outputted to the console.</li></ol>

back to the top

Complete Code Listing

 * 1) include &quot;stdafx.h&quot;


 * 1) using <mscorlib.dll>
 * 2) using <System.dll>


 * 1) include <tchar.h>

using namespace System; using namespace System::Net; using namespace System::IO;

void _tmain(void) {   String *sURL = S&quot;http://www.microsoft.com&quot;;

WebRequest *wrGETURL; wrGETURL = WebRequest::Create(sURL);

WebProxy *myProxy = new WebProxy(S&quot;myproxy&quot;, 80); myProxy->BypassProxyOnLocal = true;

//wrGETURL.Proxy = myProxy; wrGETURL->Proxy = WebProxy::GetDefaultProxy;

Stream *objStream = wrGETURL->GetResponse->GetResponseStream;

StreamReader *objReader = new StreamReader(objStream); String *sLine = S&quot;&quot;; Int32 i = 0;

while(sLine != NULL) {       i += 1; sLine = objReader->ReadLine; if(sLine != NULL) Console::WriteLine(S&quot;{0}:{1}&quot;, __box(i), sLine); }

Console::ReadLine; } back to the top

<div class="references_section">