Article ID: 815657 - Last Review: January 11, 2007 - Revision: 3.1

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

On This Page

Expand all | Collapse all

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.


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 "Request a Web Page" section.

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:
  1. Start Visual Studio .NET or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. 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:
    #using <mscorlib.dll>
    #include <tchar.h>
    #using <System.Dll> // Refers "System.Dll" file.
    
    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:
    310674  (http://support.microsoft.com/kb/310674/ ) HOW TO: Add References to a Managed Visual C++ Project
  5. 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;
  6. 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"http://www.microsoft.com";
  7. 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);
  8. 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"myproxy", 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.
  9. To use the already configured Internet Explorer settings, use the GetDefaultProxy static method of the WebProxy class, as follows:
    wrGETURL->Proxy = WebProxy::GetDefaultProxy();
  10. 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();
  11. 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"{0}:{1}", __box(i), sLine);
    }
  12. 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.

Complete Code Listing

#include "stdafx.h"

#using <mscorlib.dll>
#using <System.dll>

#include <tchar.h>

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

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

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

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

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

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

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

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

	Console::ReadLine();
}

REFERENCES

For more information, see the following book:
Templeman, Julian and Andy Olsen. Microsoft Visual C++ .NET Step by Step. Microsoft Press, 2003
For additional information, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn2.microsoft.com/en-us/visualc/aa336461.aspx (http://msdn2.microsoft.com/en-us/visualc/aa336461.aspx)

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
Keywords: 
kbhowtomaster kbio kbhtml KB815657
 

Article Translations