Libcurl的编译_HTTP/HTTPS客户端源码示例

HTTP/HTTPS客户端源码示例

环境:  zlib-1.2.8  openssl-1.0.1g  curl-7.36

Author:  Kagula

LastUpdateDate: 2016-05-09

阅读前提:CMake工具的基本使用、配置openssl-1.0.1g 开发环境

 

编译zlib库

         下载zlib-1.2.8.tar.gz并解压缩到" D:\SDK\zlib-1.2.8",使用CMake工具生成zlib.sln,在Visual Studio2013中打开并编译即可。

编译curl-7.36.0

    假设Open SSL已经安装到“D:\SDK\openssl-1.0.1g”,先设置下面的环境变量

OPENSSL_LIBRARIES=D:\SDK\openssl-1.0.1g\out32

OPENSSL_ROOT_DIR=D:\SDK\openssl-1.0.1g

    从http://curl.haxx.se/下载curl-7.36.0.zip并解压缩到“D:\SDK\curl-7.36.0”启动CMake工具Configure,分别设置LIB_EAY_RELEASESSL_EAY_RELEASE变量为“D:\SDK\openssl-1.0.1g\out32\libeay32.lib”,“D:\SDK\openssl-1.0.1g\out32\ssleay32.lib”,产生sln文件后打开,为里面的curl工程项目添加“USE_MANUAL”宏定义,然后build里面的4个项目成功。

   为项目添加链接库libcurl_imp.lib  , 把libcurl.dll文件复制到C++项目路径下,否则程序运行会提示找不到动态链接库。

下面是HTTP/HTTPS客户端示例


如何使用

#include <iostream>
#include <string>

using namespace std;

#include "httpclient.h"

int main(int argc, char *argv[])
{
    string response;

    //login
    kagula::network::CHttpClient client;
    int nR = client.Post("https://lijun:8443/escortcashbox/main/login.do",
                         "data={\"version\":\"1.0.0.0\",\"user\":\"admin\",\"password\":\"123\"}",                         
                         response,true,
                         "d:\cookie.txt",
                         "D:/workspace_qt/build-escortcashbox_c-Mingw32-Debug/debug/tomcat7.pem",
                         "D:/workspace_qt/build-escortcashbox_c-Mingw32-Debug/debug/client_all.pem",
                         "123456");
    cout << nR << endl <<response << endl;

    //upload png file
    /*
    std::map<std::string,std::string> mapFields;
    std::map<std::string,std::vector<std::string>> mapFiles;
    mapFields["data"] = "{\"version\":\"1.0.0.0\",\"socialid\":\"1012\",\"realname\":\"realnamevalue\",\"portrait\":\"protraitvalue\",\"fingerid\":\"fingeridvalue\",\"groupid\":\"123\"}";
    std::vector<std::string> vecFile(2);
    vecFile[0] = "d:\\b.png";
    vecFile[1] = "image/png";
    mapFiles["portraitFile"] = vecFile;
    nR = client.MultipartFormdata("https://lijun:8443/escortcashbox/main/escortStaffAdd.do",
                                  mapFields,mapFiles,
                                  response,
                                  "d:\cookie.txt",
                                  "D:/workspace_qt/build-escortcashbox_c-Mingw32-Debug/debug/tomcat7.pem",
                                  "D:/workspace_qt/build-escortcashbox_c-Mingw32-Debug/debug/client_all.pem",
                                  "123456");

    cout << nR << endl << response << endl;
*/
    //download png file
    nR = client.GetFile("https://lijun:8443/escortcashbox/upload/img/20160527_110514_679_426.png",
                        "d:/ee.png",
                        "d:\cookie.txt",
                        "D:/workspace_qt/build-escortcashbox_c-Mingw32-Debug/debug/tomcat7.pem",
                        "D:/workspace_qt/build-escortcashbox_c-Mingw32-Debug/debug/client_all.pem",
                        "123456");
    cout << nR << endl;

    return 0;
}


HttpClient.h封装好的头文件

//HttpClient.h源代码清单
#ifndef _HTTPCLIENT_H_
#define _HTTPCLIENT_H_

#include <string>
#include <map>
#include <vector>

/*
Title: Get Response from Web Server by HTTP/HTTPS method.
Environment:
Windows 7SP1, Windows 8.1, Windows 10
QT Creator 3.5.1, Visual Studio 2013 Update1, Visual Studio 2013 Update5
libcurl 7.36.0, libcurl 7.46.0, Qt 5.6, MSYS2 64bits gcc 5.3.0
Last Update: 2016-05-27
Remark:
[1]如果要在多线程方式下同时调用多个CHttpClient实例,
 需要在App初始化的时候调用kagula::network::Init();
 在App结束的时候调用kagula::network::Cleanup();
[2]编译libcurl必须打开zlib标志,并且把OpenSSL也链进去。

Reference:
curl_eay_setopt manual
http://www.helplib.net/s/linux.die/65_2740/man-3-curl-easy-setopt.shtml
C++ cout format
http://www.cnblogs.com/devymex/archive/2010/09/06/1818754.html
*/

namespace kagula
{
    namespace network
    {
        void Init();
        void Cleanup();

        class CHttpClient
        {
        public:
            CHttpClient(void);
            ~CHttpClient(void);

        public:
            /**
            * @brief HTTP/HTTPS POST/GET请求
            * @param strUrl 输入参数,请求的Url地址,如:https://www.alipay.com
            * @param strPost 输入参数,使用如下格式para1=val1¶2=val2&…
            * @param strCookie 输入参数,Cookie文件名,例如 d:\temp\cookie.txt
            *                          如果为空,不启用Cookie.
            * @param strResponse 输出参数,返回的内容
            * @param bPost 是否Post方式发送请求,默认Post方式发送请求。
            * @param pCaPath 输入参数,为CA证书的路径.如果输入为NULL,则不验证服务器端证书的有效性.
            * @param pClientCalPath 输入参数,为客户端证书的路径.如果输入为NULL,则不验证客户端证书的有效性.
            * @param pClientCalPassword 输入参数,为客户端证书的存取密码.
            * @return 返回是否Post成功
            * 0  成功
            *
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值