curl+openssl编译

编译win32需要使用vs命令行x86,64需使用x64
pushd D:\curl+openssl\source_code\openssl-master
perl Configure VC-WIN32 no-asm no-shared --prefix="D:\curl+openssl\source_code\openssl_bulider"
这个时候如果返回 “警告被视为误错误,,没有生成obj”,可以去根路径下找到makefile文件,搜索/wx,注释掉即可,或者按照提示更改对应文件的保存编码
/NODEFAULTLIB:msvcrtd.lib


2、用curl库访问https网页,先看下支不支持https协议,但是报这个error:CURLE_PEER_FAILED_VERIFICATION (51) – 远程服务器的 SSL 证书或 SSH md5 指纹不正确。

在网上搜罗了一番,找到这个链接:http://www.cnblogs.com/ainiaa/archive/2011/11/08/2241385.html;非常感谢作者,但是第二种方法我不能用,因为那是php的语言;但是我们可以选取第一种方法解决这个问题。

使用curl如果想发起的https请求正常的话有2种做法:

方法一、设定为不验证证书和host。

在执行curl_exec()之前。设置option

$ch = curl_init();

……

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

方法二、设定一个正确的证书。

本地ssl判别证书太旧,导致链接报错ssl证书不正确。

我们需要下载新的ssl 本地判别文件

http://curl.haxx.se/ca/cacert.pem

放到 程序文件目录

curl 增加下面的配置

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true); ;
curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).’/cacert.pem’);


// curlDemo.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include <iostream>
#include "WinHttpClient/WinHttpClient.h"
#include "httpclient.h"
using namespace std;
 
wstring UTF8ToUnicode( const string &str )
{
        int  len = 0;
        len = str.length();
        int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,
                0,
                str.c_str(),
                -1,
                NULL,
                0 );
        wchar_t   *pUnicode;
        pUnicode = new  wchar_t[unicodeLen + 1];
        memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));
        ::MultiByteToWideChar( CP_UTF8,
                0,
                str.c_str(),
                -1,
                (LPWSTR)pUnicode,
                unicodeLen );
        wstring  rt;
        rt = ( wchar_t * )pUnicode;
        delete  pUnicode;
 
        return  rt;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
        string strResponse;
        //curl CHttpClient Test
        CHttpClient client;
        client.Get("http://www.baidu.com",strResponse);
        MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://www.baidu.com",MB_OK);
        strResponse.clear();
        client.Gets("https://www.alipay.com",strResponse);
        MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"https://www.alipay.com",MB_OK);
        strResponse.clear();
         client.Get("http://so.baiduyun.me/search.php?wd=google",strResponse);
        MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://so.baiduyun.me/search.php?wd=google",MB_OK);
        strResponse.clear();
        client.Post("http://so.baiduyun.me/search.php","wd=google",strResponse);
        MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://so.baiduyun.me/search.php?wd=google",MB_OK);
 
        //winhttp WinHttpClient Test
        WinHttpClient WinClient(L"https://itunes.apple.com/cn/lookup?id=527563481");
        WinClient.SetRequireValidSslCertificates(false);
        WinClient.SendHttpRequest(L"GET");
        wstring httpResponseContent = WinClient.GetResponseContent();
        MessageBoxW(NULL,httpResponseContent.c_str(),L"http://www.baidu.com",MB_OK);
 
        return 0;

}


//预编译定义
#define CURL_STATICLIB                    //curl定义,表示我们当前使用的是lib,去掉表示当前使用dll
#define WIN32_LEAN_AND_MEAN                //系统定义,vs2017等新版编译器务必带上,否则xp平台工具集无法编译通过
 
#include <stdio.h>
#include "curl/curl.h"                    //单书名号切换双引号以找到头文件
 
#pragma comment(lib, "libcurld.lib")    //刚才编译的三个lib
#pragma comment(lib, "libcrypto.lib")
#pragma comment(lib, "libssl.lib")
 
#pragma comment(lib, "ws2_32.lib")        //系统lib,Curl与OpenSSL的lib中会用到,缺一不可
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "Crypt32.lib")
 
#define SKIP_PEER_VERIFICATION            //跳过证书验证
#define SKIP_HOSTNAME_VERIFICATION        //跳过验证hostname
 
int main(void)
{
  CURL *curl;
  CURLcode res;
 
  curl_global_init(CURL_GLOBAL_DEFAULT);
 
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
 
#ifdef SKIP_PEER_VERIFICATION
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
 
#ifdef SKIP_HOSTNAME_VERIFICATION
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
 
    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
 
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  return 0;
}

发布了12 篇原创文章 · 获赞 7 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/rqf520/article/details/103067518