Click here to Skip to main content
11,575,109 members (56,753 online)
Click here to Skip to main content

Classes for Writing HTTP Clients in C++

, 2 Jan 2009 BSD 179.8K 8K 85
Rate this:
Please Sign up or sign in to vote.
Wrapper classes for Win32 HTTP calls, URL encoding, etc.

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 supports Java, I have used Tomcat. If your web server does not support Java, write the server-script in whatever 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 walkthrough of the demo project.

First make the following include statements in the CPP file that uses 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 expects 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 sent 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 downloads 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

  • 10/20/2003: Submitted to CodeProject
  • 12/30/2008: Updated source and demo project

License

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

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionStack overflow Pin
Member 1149166027-Apr-15 22:57
memberMember 1149166027-Apr-15 22:57 
BugIt's great but do not actually works Pin
Mr.Fantastic23-Nov-14 20:31
memberMr.Fantastic23-Nov-14 20:31 
AnswerRe: It's great but do not actually works Pin
Member 1149166027-Apr-15 22:43
memberMember 1149166027-Apr-15 22:43 
GeneralMy vote of 1 Pin
highfestiva22-Jan-12 9:09
memberhighfestiva22-Jan-12 9:09 
QuestionHow can I add my edition of WebForm or send it to the author? Pin
boriskamenov15-Jul-10 22:30
memberboriskamenov15-Jul-10 22:30 
AnswerRe: How can I add my edition of WebForm or send it to the author? Pin
Vijay Mathew Pandyalakal17-Jul-10 6:58
memberVijay Mathew Pandyalakal17-Jul-10 6:58 
GeneralRe: How can I add my edition of WebForm or send it to the author? Pin
Member 766385418-Feb-11 2:59
memberMember 766385418-Feb-11 2:59 
GeneralTHANKS YOU!!!! Pin
0xFFFFSERG14-Jan-10 16:16
member0xFFFFSERG14-Jan-10 16:16 
You're my savior, thanks a lot!!!
I love you )))))
QuestionFile uploads? Pin
ArvindRSingh20-Jun-09 11:02
memberArvindRSingh20-Jun-09 11:02 
GeneralWeb Server Config Pin
Member 213022722-Jan-09 4:15
memberMember 213022722-Jan-09 4:15 
GeneralAsync Pin
jsh_ec3-Jan-09 5:34
memberjsh_ec3-Jan-09 5:34 
GeneralProblem with opening "big" sites Pin
Milos Milovanovic30-Dec-08 7:36
memberMilos Milovanovic30-Dec-08 7:36 
GeneralRe: Problem with opening "big" sites Pin
Vijay Mathew Pandyalakal30-Dec-08 17:15
memberVijay Mathew Pandyalakal30-Dec-08 17:15 
GeneralRe: Problem with opening "big" sites Pin
Milos Milovanovic1-Jan-09 2:37
memberMilos Milovanovic1-Jan-09 2:37 
GeneralRe: Problem with opening "big" sites Pin
Vijay Mathew Pandyalakal1-Jan-09 5:55
memberVijay Mathew Pandyalakal1-Jan-09 5:55 
GeneralHTTP POST Pin
anthony.kho17-Dec-08 22:15
memberanthony.kho17-Dec-08 22:15 
GeneralRe: HTTP POST Pin
Vijay Mathew Pandyalakal19-Dec-08 1:54
memberVijay Mathew Pandyalakal19-Dec-08 1:54 
GeneralRe: HTTP POST Pin
SirDiesALot17-Mar-09 6:13
memberSirDiesALot17-Mar-09 6:13 
GeneralRe: HTTP POST Pin
Vijay Mathew Pandyalakal17-Mar-09 6:45
memberVijay Mathew Pandyalakal17-Mar-09 6:45 
QuestionHow to handle file not found (HTTP Error 404) ?? Pin
AbinThomas3-Jan-08 23:33
memberAbinThomas3-Jan-08 23:33 
AnswerRe: How to handle file not found (HTTP Error 404) ?? Pin
Vijay Mathew Pandyalakal7-Jan-08 17:51
memberVijay Mathew Pandyalakal7-Jan-08 17:51 
GeneralThank you for the code + remark Pin
wakeboarderxxx19861-Nov-07 9:03
memberwakeboarderxxx19861-Nov-07 9:03 
GeneralAuthentication in HTTP clients Pin
pelis23-May-07 3:55
memberpelis23-May-07 3:55 
GeneralRe: Authentication in HTTP clients Pin
Vijay Mathew Pandyalakal23-May-07 17:38
memberVijay Mathew Pandyalakal23-May-07 17:38 
General[Linker error] undefined reference to `openutils::WebForm::WebForm()' Pin
lil_zee27-Feb-07 5:05
memberlil_zee27-Feb-07 5:05 
GeneralRe: [Linker error] undefined reference to `openutils::WebForm::WebForm()' Pin
Vijay Mathew Pandyalakal27-Feb-07 17:11
memberVijay Mathew Pandyalakal27-Feb-07 17:11 
GeneralIt does not work got all URLs Pin
Andyzyx12-Feb-07 4:37
memberAndyzyx12-Feb-07 4:37 
GeneralRe: It does not work got all URLs Pin
Vijay Mathew Pandyalakal12-Feb-07 18:56
memberVijay Mathew Pandyalakal12-Feb-07 18:56 
GeneralTo make shorter code.... Pin
frama16-May-06 2:52
memberframa16-May-06 2:52 
GeneralSmall bugfix to the URL encoder Pin
SethR10-Jul-05 20:35
memberSethR10-Jul-05 20:35 
GeneralOpening a URL in C program Pin
free_bird26-Jun-04 14:29
memberfree_bird26-Jun-04 14:29 
GeneralRe: Opening a URL in C program Pin
Ravi Bhavnani26-Jun-04 15:34
memberRavi Bhavnani26-Jun-04 15:34 
GeneralOpening a URL in C Program Pin
free_bird26-Jun-04 14:23
memberfree_bird26-Jun-04 14:23 
Questioncurl -- a portable webform? Pin
goofy.j28-Oct-03 20:52
membergoofy.j28-Oct-03 20:52 
GeneralNice, probably useful, but some remarks Pin
Patje20-Oct-03 5:43
memberPatje20-Oct-03 5:43 
GeneralRe: Nice, probably useful, but some remarks Pin
Jörgen Sigvardsson20-Oct-03 9:12
memberJörgen Sigvardsson20-Oct-03 9:12 
GeneralRe: Nice, probably useful, but some remarks Pin
Nemanja Trifunovic20-Oct-03 19:17
memberNemanja Trifunovic20-Oct-03 19:17 
GeneralRe: Nice, probably useful, but some remarks Pin
Vijay Mathew Pandyalakal21-Oct-03 3:33
memberVijay Mathew Pandyalakal21-Oct-03 3:33 
GeneralUnicode Pin
Uwe Keim20-Oct-03 5:27
sitebuilderUwe Keim20-Oct-03 5:27 
GeneralRe: Unicode Pin
John Crenshaw17-Apr-07 9:24
memberJohn Crenshaw17-Apr-07 9:24 
QuestionHTTPS? Pin
Ed K20-Oct-03 5:05
memberEd K20-Oct-03 5:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 3 Jan 2009
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid