Classes for Writing HTTP Clients in C++ CodeProject
lasses for Writing HTTP Clients in C++ - CodeProject
Classes for Writing HTTP Clients in C++
By AnOldGreenHorn |
2 Jan 2009| Article
VC6MFCDevIntermediate
Wrapper classes for Win32 HTTP calls, URL encoding, etc.See AlsoArticle Browse Code Stats Revisions (2) Alternatives
4.36 (18 votes)
4.36/5 - 18 votes2 removedμ 4.00, σa 2.39 [?]
1
2
3
4
5
Discuss this article
38Introduction
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
Collapse | Copy Codeinclude
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
Collapse | Copy CodeWebForm
class.WebForm wf;
You should tell
Collapse | Copy CodeWebForm
the name of the web server that it should connect to:wf.setHost("http://localhost:8080");
You can replace "
Collapse | Copy Codelocalhost:8080
" with any valid HTTP URL like:"www.codeproject.com" or "208.45.33.44"
The next parameter that
Collapse | Copy CodeWebForm
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
Collapse | Copy CodeInterestServlet
expects three variables or parameters:name
,rate
andamt
. These variables and their values can be added to theWebForm
object by calling theputVariable()
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
Collapse | Copy CodesendRequest()
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:
Collapse | Copy Codechar response[101]; if(wf.getResponse(response,100)) { cout << endl << response << endl; }
We can also use the
Collapse | Copy CodeWebForm
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
Collapse | Copy Codestring
in the HTTP encoding format. This class is used internally byWebForm
. 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
Collapse | Copy Codetry-catch
block that catchWebFormException
s.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
License
This article, along with any associated source code and files, is licensed under The BSD License
10/20/2003: Submitted to CodeProject
12/30/2008: Updated source and demo project
Download demo project - 8.71 KB
Download source - 2.9 KB
More like this
More by this author
相关阅读:
TCHAR转化为UTF8
Chromium ID 计算方法
构建之法感悟
项目名称:上海地铁游
统一入口的Ajax验证
基于用户体验的服务型政府网站群建设优化方案
新技术下的政府门户网站群建设建议
国内CMS技术发展的外在表现形式
JavaScript基础 BOM
JavaScript基础 常见DOM树操作原文地址:https://www.cnblogs.com/lexus/p/2594627.html
最新文章
关于DirectX中的DirectShow介绍
用jQuery UI做简单九宫格拼图
非网页安全字体不再依赖Cufon!CSS @fontface的使用
通过jQuery调用ASP.NET的AJAX
Magento网店自定义模板初探(4)——Package读取机制
Bug笔记:webkitappearance:none导致无法获取checkbox值
Magento网店自定义模板初探(3)——缓存设置
Magento网店自定义模板初探(2)——开启Debug模式
设置viewport后Colorbox在iPhone中不能居中显示
关于浏览器地址栏和标题栏显示的小图标
热门文章
Javascript的Image对象、图像渲染与浏览器内存两三事
单例模式(Singleton)
MDIForm工具栏中的按键事件与子窗体的btn事件
Membership角色与权限管理
JSON 入门指南
SQL性能优化
Node.js调试工具安装使用
DER证书读取保存和转换
无线网络IP设置 批处理
读取文件中的内容