最近项目有需求要用c++发送http请求,找了一个开源的叫happyhttp,看了一下例子是c形式的用法,不是很习惯,改造了一下增加了一个CMyHappyHttp的class变为c++形式的。happyhttp官网:http://scumways.com/happyhttp/happyhttp.html。本人主要是java的,c++人手不够也帮忙做做,如有不好的地方,欢迎指正。代码请参看附件。
测试代码:
#include "happyhttp.h"
#include <cstdio>
#include <cstring>
#ifdef WIN32
#include <winsock2.h>
#endif // WIN32
void Test()
{
printf("-----------------Test------------------------\n" );
happyhttp::CMyHappyHttp cMyHappyHttp;
string strPf = "/9j/4AAQSkZJRgABAQEAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU";
strPf = cMyHappyHttp.urlEncoding(strPf);
string strCustCertAddr = "";
string strCustName = "小明";
string strIssuingAuthority = "广州天河公安局";
string strNation = "汉";
string strErr = "";
int iRet = cMyHappyHttp.myConvertCharset("什么", "GBK", strCustCertAddr, "UTF-8", strErr);
cout << "▇iRet:" << iRet << endl;
cout << "▇strErr:" << strErr << endl;
string strBody = "params={\"busiCode\":\"OPEN_CALL\",\"reqInfo\":{\"billId\":\"13507884478\",\"birthday\":\"19890210\",\"busiType\":\"11\",\"certExpdate\":\"20180722\",\"certValiddate\":\"20080722\",\"channelId\":\"onlineball\",\"custCertAddr\":\"" + strCustCertAddr + "\",\"custCertNo\":\"450422198902102656\",\"custName\":\"" + strCustName + "\",\"gender\":\"0\",\"issuingAuthority\":\"" + strIssuingAuthority + "\",\"nation\":\"" + strNation + "\",\"operCode\":\"\",\"picNameF\":\"" + strPf + "\",\"picNameT\":\"\",\"picNameZ\":\"\",\"transactionID\":\"77100120151008173847000404\"},\"sourceCode\":\"1085\",\"targetCode\":\"771\",\"version\":\"1.0\"}";
string strResponse = "";
strErr = "";
iRet = cMyHappyHttp.postHttpRequest("http://10.182.20.38:9808/open/workflow/realNameInter", strBody.c_str(), strResponse, strErr);
cout << "▇iRet:" << iRet << endl;
cout << "▇strErr:" << strErr << endl;
cout << "▇strResponse:" << strResponse << endl;
}
int main( int argc, char* argv[] )
{
#ifdef WIN32
WSAData wsaData;
int code = WSAStartup(MAKEWORD(1, 1), &wsaData);
if( code != 0 )
{
printf("shite. %d\n",code);
return 0;
}
#endif //WIN32
try
{
Test();
}
catch( happyhttp::Wobbly& e )
{
printf("Exception:\n%s\n", e.what() );
}
#ifdef WIN32
WSACleanup();
#endif // WIN32
return 0;
}
运行结果:
-----------------Test------------------------
▇iconvConvertCharset-in:什么
▇iconvConvertCharset-out:浠€涔?
▇iconvConvertCharset-ol:494
▇iRet:0
▇strErr:
▇server:10.182.20.38
▇port:9808
▇path:/open/workflow/realNameInter
▇body:params={"busiCode":"OPEN_CALL","reqInfo":{"billId":"13507884478","birthday":"19890210","busiType":"11","certExpdate":"20180722","certValiddate":"20080722","channelId":"onlineball","custCertAddr":"浠€涔?,"custCertNo":"450422198902102656","custName":"小明","gender":"0","issuingAuthority":"广州天河公安局","nation":"汉","operCode":"","picNameF":"%2F9j%2F4AAQSkZJRgABAQEAAQABAAD%2F2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU","picNameT":"","picNameZ":"","transactionID":"77100120151008173847000404"},"sourceCode":"1085","targetCode":"771","version":"1.0"}
▇BEGIN (200 OK)
{"returnCode":"0000","returnMessage":"成功"}▇COMPLETE (44 bytes)
▇iRet:0
▇strErr:
▇strResponse:{"returnCode":"0000","returnMessage":"成功"}