vs2013 配置支持https的libcurl

栏目: 服务器 · 发布时间: 7年前

内容简介:需求:在vs2013环境:win7(64

需求:在vs2013 上配置支持 https 协议的 libcurl

环境:win7(64 ) vs2013

一、 安装 openssl

需要先安装 nasm和ActivePerl。

nasm需要手动配置环境变量。ActivePerl 下载下来一路 next安装就好了。

我安装 openssl 的时候是 2018-08-03 。不保证以后下面这个链接的方法依然有用。

我参考的是这个: https://blog.csdn.net/hpp24/article/details/54406424

亲测有用。只不过生成的 include lib 等目录不对。居然在这目录C:\usr\local\ssl

需要注意的是,由于我的系统是 64位的。我的vcvars32.bat在C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin这个目录中。

使用时需要指定头文件路径:

C:\usr\local\ssl\include

库的路径:

C:\usr\local\ssl\lib

同时要把 libeay32.dll ssleay32.dll 拷贝到lib目录下。

使用时要把上面两个 dll跟可执行文件放在一起。

#pragma comment(lib, " ssleay32.lib ")

#pragma comment(lib, " libeay32.lib ")

二、 编译 libcurl

直接去下载最新的。 https://curl.haxx.se/download.html

我下载的是这个 curl-7.61.0

解压,之后进入 \curl-7.61.0\projects\Windows\VC12 ,打开 curl-all.sln 。里面有两个工程,

vs2013 配置支持https的libcurl

选择libcurl 。配置选下面这个:

vs2013 配置支持https的libcurl

配置属性 - VC++ 目录 - 》包含目录  要指定 openssl 的头文件路径。

配置属性 - VC++ 目录 - 》库目录 要指定 openssl 的库目录。

然后直接编译就好了。

curl-7.61.0\build\Win32\VC12\DLL Release - DLL OpenSSL

这个目录下会生成 libcurl.lib  libcurl.dll 。这就大功告成了。

使用时的头文件路径是 \curl-7.61.0\include

三、测试

可以参考这个: https://curl.haxx.se/libcurl/c/https.html

下面上我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<curl/curl.h>
#include<iostream>

using namespace std;

#pragma comment(lib, "libcurl.lib")
#pragma comment(lib, "ssleay32.lib")
#pragma comment(lib, "libeay32.lib")

void test2()
{
    curl_global_init(CURL_GLOBAL_DEFAULT);

    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if (curl) 
    {
        /* First set the URL that is about to receive our POST. This URL can
        just as well be a https:// URL if that is what should receive the
        data. */
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com");

        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);//忽略证书检查
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

        /* Now specify the POST data */
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        if (res)
        {
            cout << "curl_easy_perform failed res = " << res << " ErrInfo:" << curl_easy_strerror(res) << endl;
        }
        else
        {
            cout << "curl_easy_perform success " << endl;
        }
        
        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
}

int main()
{
    test2();
    getchar();
    return 0;
}

要配置 curl 的头文件路径和库路径,和 openssl 的库路径。

直接运行就好了。

四、总结

1 )编译 openssl 比较费时间,这一步一定要做,不要想偷懒,按步骤一步一步来就好了。

2 )运行 curl 测试程序时需要链接 libcurl openssl 的那两个库。特别注意要把库放到程序运行目录中去。我最开始怀疑自己是不是没编译到包含 openssl libcurl 。一直在库目录上折腾,没有放到运行目录中去,折腾了将近一天。

3 )查看自己编译的 libcurl 是否支持 https ,可以编译 curl-all.sln 中的 curl 项目,配置一样,要选 openssl 的。然后会编译出一个 curl.exe 。然后打开系统自带的 cmd.exe ,切换到生成 curl.exe 的目录,把两个 openssl 的库拷贝进来。执行红色框两个命令。出现下面的结果就表示你成功了。

vs2013 配置支持https的libcurl


以上所述就是小编给大家介绍的《vs2013 配置支持https的libcurl》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

R Cookbook

R Cookbook

Paul Teetor / O'Reilly Media / 2011-3-22 / USD 39.99

With more than 200 practical recipes, this book helps you perform data analysis with R quickly and efficiently. The R language provides everything you need to do statistical work, but its structure ca......一起来看看 《R Cookbook》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具