方法上个人更倾向于简单的方法,更方便教给别人,而且这部分不是重点,不应该占用太多时间,重点是后面使用。
1.首先尝试了NuGet,安装了curl,但是显示不能识别函数。像是没有库文件,没有找到原因,如果有人找到原因请联系我,我把这篇文章改成用NuGet安装。
2.在网上看了很多文章,需要先编译,编译curl前还要先编译openssl,还有依赖,很麻烦。所以决定直接找编译好的。最后锁定这篇文章:《C++(学习笔记)——VS2015静态编译libcurl》,下面基于这篇文章来写
3.直接操作文章的第3步,下载编译好的libcurl,他的这个库需要的积分较多,可以去找找有没有少一点的
4.直接操作文章中:二简单使用教程的1,2,3,4步依次操作
5.测试代码用我的,因为在测试时,发现没有通过CA验证,所以屏蔽了curl的验证功能
#include <stdio.h>
#include <curl/curl.h>
#define SKIP_PEER_VERIFICATION
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, "http://www.baidu.com");
#ifdef SKIP_PEER_VERIFICATION
/*
* If you want to connect to a site who isn't using a certificate that is
* signed by one of the certs in the CA bundle you have, you can skip the
* verification of the server's certificate. This makes the connection
* A LOT LESS SECURE.
*
* If you have a CA cert for the server stored someplace else than in the
* default bundle, then the CURLOPT_CAPATH option might come handy for
* you.
*/
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
#ifdef SKIP_HOSTNAME_VERIFICATION
/*
* If the site you're connecting to uses a different host name that what
* they have mentioned in their server certificate's commonName (or
* subjectAltName) fields, libcurl will refuse to connect. You can skip
* this check, but this will make the connection less secure.
*/
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
显示如上信息,说明成功抓取到网站的信息,成功