5,696,576 members and growing! (15,976 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate License: The BSD License

Classes for writing HTTP clients in C++

By Vijay Mathew Pandyalakal

Wrapper classes for Win32 HTTP calls,URL encoding etc
VC6, C++Windows, NT4, MFC, VS6, Visual Studio, Dev

Posted: 19 Oct 2003
Updated: 19 Oct 2003
Views: 79,928
Bookmarked: 45 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 4.45 Rating: 4.12 out of 5
1 vote, 8.3%
1
0 votes, 0.0%
2
2 votes, 16.7%
3
3 votes, 25.0%
4
6 votes, 50.0%
5

Introduction

Today, more and more developers want to write distributed transactional applications for the enterprise and leverage the speed, security, and reliability of server-side technology. Java has become the choice of thousands of developers for writing scalable programs (usually as servlets or JSP) that reside on web servers and execute business methods for clients. (We won't be talking about EJBs here !!)

These clients are usually HTML forms or Java applets that run within a web browser. What if your legacy C++ application wants to go thin, and hand-over all hard jobs like running business logic, accessing database etc to a Java servlet? How are you going to handle communication over HTTP, URL encoding variables and all that ? The classes we are going to discuss in this article will show you how easy it is to write HTTP clients in C++, even easier than writing a Java Applet for the same purpose !! You take my word for that.

Background

You must be familiar with OOPs, internet protocols etc. If you have developed a web application using Java, ASP or some other server side technology, then you will find this article easy to follow. To run the demo code, you must have access to a web server that support Java, I have used Tomcat. If your web server do not support Java, write the server-script in what ever language your web server can interpret and change the server-script's name in the demo code.

Using the code

This section is actually a walk through of the demo project.

First make the following include statements in the cpp file that use our HTTP classes:

// some standard headers

#include <string>

#include <vector>

#include <iostream>

using namespace std;
// windows headers

#include <windows.h>

#include <wininet.h>

// our header

#include "web.h"

using namespace openutils;

The program must also be linked with wininet.lib. In VC++ you can do this by going to Project->Settings->Link->Object/Library Modules and adding wininet.lib to the end of the default library names.

Next, we declare an object of the WebForm class.

WebForm wf;
You should tell WebForm the name of the web server that it should connect to:
wf.setHost("http://localhost:8080");

You can replace "localhost:8080" with any valid HTTP URL like:

"www.codeproject.com" or "208.45.33.44"

The next parameter that WebForm expect is the name of the script file that the web server should execute:
wf.setScriptFile("/banking/servlet/InterestServlet");

Please check out your web server's documentation for the path of your servlet files. On a Tomcat server, create the folder hierarchy "banking\WEB-INF\classes" in the "webapps" sub-folder and place the IntersetServlet.class file in that.

Now you are ready to add variables to be send to the servlet for processing. The "InterestServlet" expects three variables or parameters: name, rate and amt. These variables and their values can be added to the WebForm object by calling the putVariable() function.

wf.putVariable("name","James Dean");
wf.putVariable("rate","14");
wf.putVariable("amt","1200.89");

We can send an HTTP request to the servlet by calling the sendRequest() function.
wf.sendRequest();
The servlet will send back a response that will contain the calculated interest rate in HTML format. This response can be read into a buffer on the client side:
char response[101];            
if(wf.getResponse(response,100)) 
{
    cout << endl << response << endl;
}

We can also use the WebForm class to write simple clients that act like a web-browser. Here is a code snippet that download the home page of a well-known web site:

wf.setHost(http://www.microsoft.com);

wf.setScriptFile("/default.html"); 
// or wf.setScriptFile(""); 

wf.sendRequest();
char buff[10 * 1024];
if(wf.getResponse(buff,sizeof(buff))) 
{
    cout << buff << endl;
}
else 
{
    cout << "No response from server" << endl;
}

Notes

web.h defines a utility class for encoding a string in the HTTP encoding format. This class is used internally by WebForm. The usage of this class and the decoder class is demonstrated below:

string str = "AC-0099";
string str_enc = URLEncoder::encode(str); // AC%2d0099

string dec = URLDecoder::decode(str_enc); // AC-0099

Please remember to enclose all web.h function calls in a try-catch block that catch WebFormExceptions.
try {// web.h calls

} catch(WebFormException ex)
{ 
  cout << ex.getMessage(); 
}

Before running the demo, copy InterestServlet.class from the servlet folder to the appropriate script folder of your Java enabled web-server.

History

  • Submitted to CodeProject : 10/20/2003

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

Vijay Mathew Pandyalakal



Occupation: Software Developer
Location: India India

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralHow to handle file not found (HTTP Error 404) ??memberAbinThomas0:33 4 Jan '08  
GeneralRe: How to handle file not found (HTTP Error 404) ??memberVijay Mathew Pandyalakal18:51 7 Jan '08  
GeneralThank you for the code + remarkmemberwakeboarderxxx198610:03 1 Nov '07  
GeneralAuthentication in HTTP clientsmemberpelis4:55 23 May '07  
GeneralRe: Authentication in HTTP clientsmemberVijay Mathew Pandyalakal18:38 23 May '07  
General[Linker error] undefined reference to `openutils::WebForm::WebForm()'memberlil_zee6:05 27 Feb '07  
GeneralRe: [Linker error] undefined reference to `openutils::WebForm::WebForm()'memberVijay Mathew Pandyalakal18:11 27 Feb '07  
GeneralIt does not work got all URLsmemberAndyzyx5:37 12 Feb '07  
GeneralRe: It does not work got all URLsmemberVijay Mathew Pandyalakal19:56 12 Feb '07  
GeneralTo make shorter code....memberframa3:52 16 May '06  
GeneralSmall bugfix to the URL encodermemberSethR21:35 10 Jul '05  
GeneralOpening a URL in C programmemberfree_bird15:29 26 Jun '04  
GeneralRe: Opening a URL in C programmemberRavi Bhavnani16:34 26 Jun '04  
GeneralOpening a URL in C Programmemberfree_bird15:23 26 Jun '04  
Generalcurl -- a portable webform?membergoofy.j21:52 28 Oct '03  
GeneralNice, probably useful, but some remarksmemberPatje6:43 20 Oct '03  
GeneralRe: Nice, probably useful, but some remarksmemberJörgen Sigvardsson10:12 20 Oct '03  
GeneralRe: Nice, probably useful, but some remarksmemberNemanja Trifunovic20:17 20 Oct '03  
GeneralRe: Nice, probably useful, but some remarksmemberVijay Mathew Pandyalakal4:33 21 Oct '03  
GeneralUnicodesitebuilderUwe Keim6:27 20 Oct '03  
GeneralRe: UnicodememberJohn Crenshaw10:24 17 Apr '07  
GeneralHTTPS?memberEd K6:05 20 Oct '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Oct 2003
Editor: Nishant Sivakumar
Copyright 2003 by Vijay Mathew Pandyalakal
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project