精易论坛

标题: 分享一份 libcurl/7.70.0 OpenSSL/1.1.1g 网页访问模块 [打印本页]

作者: q1293847140    时间: 2020-8-6 08:33
标题: 分享一份 libcurl/7.70.0 OpenSSL/1.1.1g 网页访问模块
就简单封装了一下子, 支持ssl, 支持post/get访问, 唯一缺点就是cookies需要自己处理比较麻烦, 其他还好!  libcurl还是非常优秀的!

E.rar

1.46 MB, 下载次数: 404, 下载积分: 精币 -2 枚


作者: futiem    时间: 2020-8-6 10:57
封装成DLL文件,学习意义下降!
作者: q1293847140    时间: 2020-8-6 11:21
本帖最后由 q1293847140 于 2020-8-6 11:39 编辑
futiem 发表于 2020-8-6 10:57
封装成DLL文件,学习意义下降!

11111111111111111111111111111111111111111111
作者: q1293847140    时间: 2020-8-7 12:38
朋友们不好意思, 前面那个版本编译的调试版我没注意到, 现在重新发下, 优化了很多!  另外
dll源码为:
  1. // dll_libcurl.cpp : 定义 DLL 应用程序的导出函数。
  2. //

  3. #include "stdafx.h"

  4. struct MemoryStruct {
  5.         std::vector<char>memory;
  6.         size_t size;
  7. };

  8. extern "C" __declspec(dllexport) int __stdcall curl_global_init_(int flags)
  9. {
  10.         int ret = curl_global_init(flags);
  11.         return ret;
  12. }

  13. extern "C" __declspec(dllexport) void __stdcall curl_global_cleanup_()
  14. {
  15.         curl_global_cleanup();
  16. }


  17. //网页响应内容回调
  18. static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
  19. {
  20.         size_t realsize = size * nmemb;
  21.         struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  22.         mem->memory.reserve(realsize);
  23.         for (size_t i = 0; i < realsize; i++)
  24.         {
  25.                 mem->memory.push_back(((char *)contents)[i]);
  26.         }

  27.         mem->size += realsize;
  28.         return realsize;
  29. }

  30. //网页响应协议头回调
  31. static size_t HeaderCallback(void *contents, size_t size, size_t nmemb, void *userp)
  32. {

  33.         size_t realsize = size * nmemb;
  34.         struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  35.         mem->memory.reserve(realsize);
  36.         for (size_t i = 0; i < realsize; i++)
  37.         {
  38.                 mem->memory.push_back(((char *)contents)[i]);
  39.         }

  40.         mem->size += realsize;
  41.         return realsize;
  42. }


  43. extern "C" __declspec(dllexport) int __stdcall GetUrlHTTP(char *url, char *ret_response, int time_out, char *header, char *proxy,
  44.         char* ret_header, int *ret_header_size, int *ret_content_size, bool Redirect)
  45. {
  46.         CURL *curl;
  47.         CURLcode res;

  48.         curl = curl_easy_init();

  49.         if (curl)
  50.         {
  51.                 MemoryStruct buffer, rheader;
  52.                 buffer.size = 0;
  53.                 rheader.size = 0;

  54.                 //curl_easy_setopt(curl, CURLOPT_HEADER, 1);//设为1,则在返回的内容里包含http header;

  55.                 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, (int)Redirect);//跟踪重定向
  56.                 curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);    /*timeout 30s,add by edgeyang*/
  57.                 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);    /*no signal,add by edgeyang*/

  58.                 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");    //初始化cookie引擎,才能正确接收到cookie数据.
  59.                                                                                                                    //curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie_open.txt");

  60.                 curl_easy_setopt(curl, CURLOPT_PROXY, proxy);//设置代理
  61.                 curl_easy_setopt(curl, CURLOPT_URL, url);//设置链接
  62.                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  63.                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  64.                 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &rheader);
  65.                 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, HeaderCallback);

  66.                 //忽略 ssl验证
  67.                 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  68.                 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

  69.                 curl_slist *headers = NULL;//创建协议头列表
  70.                 if (header)
  71.                 {
  72.                         //分割出协议头进行添加
  73.                         char *token;
  74.                         char s[4] = "\r\n";
  75.                         token = strtok(header, s);
  76.                         while (token)
  77.                         {
  78.                                 //std::cout << token << std::endl;
  79.                                 headers = curl_slist_append(headers, token);
  80.                                 token = strtok(NULL, s);
  81.                         }
  82.                 }

  83.                 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  84.                 res = curl_easy_perform(curl);//执行请求
  85.                 if (headers)
  86.                 {
  87.                         curl_slist_free_all(headers);
  88.                 }

  89.                 //std::cout << rheader.memory << std::endl;
  90.                 //std::cout << buffer.memory << std::endl;


  91.                 if (ret_response)
  92.                 {
  93.                         memset(ret_response, 0, sizeof(ret_response));
  94.                         std::copy(buffer.memory.begin(), buffer.memory.end(), ret_response);
  95.                 }

  96.                 if (ret_header)
  97.                 {
  98.                         memset(ret_header, 0, sizeof(ret_header));
  99.                         std::copy(rheader.memory.begin(), rheader.memory.end(), ret_header);
  100.                 }

  101.                 if (ret_header_size)
  102.                 {
  103.                         *ret_header_size = rheader.size;
  104.                 }

  105.                 if (ret_content_size)
  106.                 {
  107.                         *ret_content_size = buffer.size;
  108.                 }

  109.                 curl_easy_cleanup(curl);

  110.         }

  111.         return res;
  112. }

  113. extern "C" __declspec(dllexport) int __stdcall PostUrlHTTP(char *url, char* PostData, char *ret_response, int time_out, char *header, char *proxy,
  114.         char* ret_header, int *ret_header_size, int *ret_content_size, bool Redirect)
  115. {
  116.         CURL *curl;
  117.         CURLcode res;

  118.         curl = curl_easy_init();

  119.         if (curl)
  120.         {
  121.                 MemoryStruct buffer, rheader;
  122.                 buffer.size = 0;
  123.                 rheader.size = 0;


  124.                 //curl_easy_setopt(curl, CURLOPT_HEADER, 1);//设为1,则在返回的内容里包含http header;

  125.                 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, (int)Redirect);//跟踪重定向
  126.                 curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out);    /*timeout 30s,add by edgeyang*/
  127.                 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);    /*no signal,add by edgeyang*/

  128.                 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");    //初始化cookie引擎,才能正确接收到cookie数据.
  129.                                                                                                                    //curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie_open.txt");

  130.                 curl_easy_setopt(curl, CURLOPT_PROXY, proxy);//设置代理
  131.                 curl_easy_setopt(curl, CURLOPT_URL, url);//设置链接
  132.                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  133.                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  134.                 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &rheader);
  135.                 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, HeaderCallback);
  136.                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, PostData); //POST方式访问并且指定数据

  137.                                                                                                                           //忽略ssl验证
  138.                 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  139.                 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

  140.                 curl_slist *headers = NULL;//创建协议头列表
  141.                 if (header)
  142.                 {
  143.                         //分割出协议头进行添加
  144.                         char *token;
  145.                         char s[4] = "\r\n";
  146.                         token = strtok(header, s);
  147.                         while (token)
  148.                         {
  149.                                 //std::cout << token << std::endl;
  150.                                 headers = curl_slist_append(headers, token);
  151.                                 token = strtok(NULL, s);
  152.                         }
  153.                 }

  154.                 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

  155.                 res = curl_easy_perform(curl);//执行请求
  156.                 if (headers)
  157.                 {
  158.                         curl_slist_free_all(headers);
  159.                 }


  160.                 //std::cout << rheader.memory << std::endl;
  161.                 //std::cout << buffer.memory << std::endl;

  162.                 if (ret_response)
  163.                 {
  164.                         memset(ret_response, 0, sizeof(ret_response));
  165.                         std::copy(buffer.memory.begin(), buffer.memory.end(), ret_response);
  166.                 }

  167.                 if (ret_header)
  168.                 {
  169.                         memset(ret_header, 0, sizeof(ret_header));
  170.                         std::copy(rheader.memory.begin(), rheader.memory.end(), ret_header);
  171.                 }

  172.                 if (ret_header_size)
  173.                 {
  174.                         *ret_header_size = rheader.size;
  175.                 }

  176.                 if (ret_content_size)
  177.                 {
  178.                         *ret_content_size = buffer.size;
  179.                 }


  180.                 curl_easy_cleanup(curl);

  181.         }

  182.         return res;

  183. }
复制代码



e1.rar

1.38 MB, 下载次数: 127, 下载积分: 精币 -2 枚


作者: 杨明煜    时间: 2020-8-20 08:35
学习一下.............
作者: jingyi11023995    时间: 2020-8-31 18:59
本帖最后由 jingyi11023995 于 2020-8-31 19:06 编辑

楼主,漏了什么dll文件或者模块吧?


我在虚拟机XP系统试的,可能是系统问题

作者: q1293847140    时间: 2020-9-1 18:50
jingyi11023995 发表于 2020-8-31 18:59
楼主,漏了什么dll文件或者模块吧?

没试过xp, 这个问题估计百度下libcurl xp方面应该能解决, 我测试win7 win10 都可以, 还有你用帖子后面那份dll
作者: 夜莫离    时间: 2020-9-1 18:53
q1293847140 发表于 2020-8-7 12:38
朋友们不好意思, 前面那个版本编译的调试版我没注意到, 现在重新发下, 优化了很多!  另外
dll源码为:

封装成DLL文件,学习意义下降!
作者: bombking    时间: 2020-10-11 22:25

封装成DLL文件,学习意义下降
作者: q1293847140    时间: 2020-11-12 16:39
本帖最后由 q1293847140 于 2020-11-12 16:47 编辑

2020/11/12 终结版
本次更新内容:
1. 完美支持XP
2. 单文件dll


TX哈勃分析地址:  https://habo.qq.com/file/showdetail?pk=ADcGY11tB2IIOFs8U2M%3D
如果大家需要dll源码的话请看回复!

完美支持xp.png (377.86 KB, 下载次数: 1)

完美支持xp.png

E CURL.rar

1.05 MB, 下载次数: 143, 下载积分: 精币 -2 枚


作者: q1293847140    时间: 2020-11-12 17:05
jingyi11023995 发表于 2020-8-31 18:59
楼主,漏了什么dll文件或者模块吧?

朋友你好, 已经全部修复, 重构工程了全部静态MT编译, 支持系统xp到win10
作者: Hugoyu    时间: 2020-11-14 21:02
支持异步回调么大佬
作者: q1293847140    时间: 2020-11-14 21:31
Hugoyu 发表于 2020-11-14 21:02
支持异步回调么大佬

不支持哈,  没去研究
作者: jingyi11023995    时间: 2020-11-15 21:18
看看更新了什么
作者: zxh1220    时间: 2020-12-20 20:52
CURL 访问网络 很慢,这个有什么好办法 吗
作者: 2161045897    时间: 2021-1-31 12:53
感谢分享~~~~~~~~~~~~~
作者: eworm    时间: 2021-2-1 20:59

作者: huxian    时间: 2021-2-8 19:55
你这个不完善啊
作者: duanyijun    时间: 2022-4-5 10:24
回帖是美德
作者: dingdongqiang    时间: 2022-4-9 19:58
没代理账号密码 中评
作者: 爱易编程    时间: 2022-6-11 12:04
谢谢分享啊
作者: ccc9    时间: 2022-6-14 17:09
学习了,谢谢
作者: xl970525    时间: 2022-6-23 11:50
支持ssl, 支持post/get访问, 唯一缺点就是cookies需要自己处理比较麻烦, 其他还好!  libcurl还是非常优秀的!
作者: superice    时间: 2023-2-12 23:05

作者: foudiewang    时间: 2023-2-25 16:58
谢谢分享学习学习了
作者: 在看风云    时间: 2023-10-19 09:20
666666666666666
作者: futiem    时间: 2024-5-8 16:54
支持ssl, 支持post/get访问,
作者: futiem    时间: 2024-5-8 16:55
其他还好!  libcurl还是非常优秀的!
作者: wbc023    时间: 2024-10-21 16:56
试试这个
作者: a1041628533    时间: 2024-12-31 17:46
感谢分享,很给力!~
作者: 951902929    时间: 2024-12-31 21:55
学习一下
作者: wlm19820    时间: 2025-2-8 00:52

其他还好!  libcurl还是非常优秀的!
作者: wlm19820    时间: 2025-2-8 00:52

谢谢分享学习学习了




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4