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.
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:
using namespace std;
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
You should tell
WebForm the name of the web server that it should connect to:
You can replace "
localhost:8080" with any valid HTTP URL like:
"www.codeproject.com" or "22.214.171.124"
The next parameter that
WebForm expects is the name of the script file that the web server should execute:
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:
amt. These variables and their values can be added to the
WebForm object by calling the
We can send an HTTP request to the servlet by calling the
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:
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:
char buff[10 * 1024];
cout << buff << endl;
cout << "No response from server" << endl;
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);
string dec = URLDecoder::decode(str_enc);
Please remember to enclose all web.h function calls in a
try-catch block that catch
} 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.
- 10/20/2003: Submitted to CodeProject
- 12/30/2008: Updated source and demo project