|
楼主 |
发表于 2020-8-7 12:38:10
|
显示全部楼层
湖南省永州市
朋友们不好意思, 前面那个版本编译的调试版我没注意到, 现在重新发下, 优化了很多! 另外
dll源码为:- // dll_libcurl.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include "stdafx.h"
- struct MemoryStruct {
- std::vector<char>memory;
- size_t size;
- };
- extern "C" __declspec(dllexport) int __stdcall curl_global_init_(int flags)
- {
- int ret = curl_global_init(flags);
- return ret;
- }
- extern "C" __declspec(dllexport) void __stdcall curl_global_cleanup_()
- {
- curl_global_cleanup();
- }
- //网页响应内容回调
- static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
- {
- size_t realsize = size * nmemb;
- struct MemoryStruct *mem = (struct MemoryStruct *)userp;
- mem->memory.reserve(realsize);
- for (size_t i = 0; i < realsize; i++)
- {
- mem->memory.push_back(((char *)contents)[i]);
- }
- mem->size += realsize;
- return realsize;
- }
- //网页响应协议头回调
- static size_t HeaderCallback(void *contents, size_t size, size_t nmemb, void *userp)
- {
- size_t realsize = size * nmemb;
- struct MemoryStruct *mem = (struct MemoryStruct *)userp;
- mem->memory.reserve(realsize);
- for (size_t i = 0; i < realsize; i++)
- {
- mem->memory.push_back(((char *)contents)[i]);
- }
- mem->size += realsize;
- return realsize;
- }
- extern "C" __declspec(dllexport) int __stdcall GetUrlHTTP(char *url, char *ret_response, int time_out, char *header, char *proxy,
- char* ret_header, int *ret_header_size, int *ret_content_size, bool Redirect)
- {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if (curl)
- {
- MemoryStruct buffer, rheader;
- buffer.size = 0;
- rheader.size = 0;
- //curl_easy_setopt(curl, CURLOPT_HEADER, 1);//设为1,则在返回的内容里包含http header;
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, (int)Redirect);//跟踪重定向
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out); /*timeout 30s,add by edgeyang*/
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); /*no signal,add by edgeyang*/
- curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); //初始化cookie引擎,才能正确接收到cookie数据.
- //curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie_open.txt");
- curl_easy_setopt(curl, CURLOPT_PROXY, proxy);//设置代理
- curl_easy_setopt(curl, CURLOPT_URL, url);//设置链接
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- curl_easy_setopt(curl, CURLOPT_HEADERDATA, &rheader);
- curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, HeaderCallback);
- //忽略 ssl验证
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_slist *headers = NULL;//创建协议头列表
- if (header)
- {
- //分割出协议头进行添加
- char *token;
- char s[4] = "\r\n";
- token = strtok(header, s);
- while (token)
- {
- //std::cout << token << std::endl;
- headers = curl_slist_append(headers, token);
- token = strtok(NULL, s);
- }
- }
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
- res = curl_easy_perform(curl);//执行请求
- if (headers)
- {
- curl_slist_free_all(headers);
- }
- //std::cout << rheader.memory << std::endl;
- //std::cout << buffer.memory << std::endl;
- if (ret_response)
- {
- memset(ret_response, 0, sizeof(ret_response));
- std::copy(buffer.memory.begin(), buffer.memory.end(), ret_response);
- }
- if (ret_header)
- {
- memset(ret_header, 0, sizeof(ret_header));
- std::copy(rheader.memory.begin(), rheader.memory.end(), ret_header);
- }
- if (ret_header_size)
- {
- *ret_header_size = rheader.size;
- }
- if (ret_content_size)
- {
- *ret_content_size = buffer.size;
- }
- curl_easy_cleanup(curl);
- }
- return res;
- }
- extern "C" __declspec(dllexport) int __stdcall PostUrlHTTP(char *url, char* PostData, char *ret_response, int time_out, char *header, char *proxy,
- char* ret_header, int *ret_header_size, int *ret_content_size, bool Redirect)
- {
- CURL *curl;
- CURLcode res;
- curl = curl_easy_init();
- if (curl)
- {
- MemoryStruct buffer, rheader;
- buffer.size = 0;
- rheader.size = 0;
- //curl_easy_setopt(curl, CURLOPT_HEADER, 1);//设为1,则在返回的内容里包含http header;
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, (int)Redirect);//跟踪重定向
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, time_out); /*timeout 30s,add by edgeyang*/
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); /*no signal,add by edgeyang*/
- curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); //初始化cookie引擎,才能正确接收到cookie数据.
- //curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie_open.txt");
- curl_easy_setopt(curl, CURLOPT_PROXY, proxy);//设置代理
- curl_easy_setopt(curl, CURLOPT_URL, url);//设置链接
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
- curl_easy_setopt(curl, CURLOPT_HEADERDATA, &rheader);
- curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, HeaderCallback);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, PostData); //POST方式访问并且指定数据
- //忽略ssl验证
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
- curl_slist *headers = NULL;//创建协议头列表
- if (header)
- {
- //分割出协议头进行添加
- char *token;
- char s[4] = "\r\n";
- token = strtok(header, s);
- while (token)
- {
- //std::cout << token << std::endl;
- headers = curl_slist_append(headers, token);
- token = strtok(NULL, s);
- }
- }
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
- res = curl_easy_perform(curl);//执行请求
- if (headers)
- {
- curl_slist_free_all(headers);
- }
- //std::cout << rheader.memory << std::endl;
- //std::cout << buffer.memory << std::endl;
- if (ret_response)
- {
- memset(ret_response, 0, sizeof(ret_response));
- std::copy(buffer.memory.begin(), buffer.memory.end(), ret_response);
- }
- if (ret_header)
- {
- memset(ret_header, 0, sizeof(ret_header));
- std::copy(rheader.memory.begin(), rheader.memory.end(), ret_header);
- }
- if (ret_header_size)
- {
- *ret_header_size = rheader.size;
- }
- if (ret_content_size)
- {
- *ret_content_size = buffer.size;
- }
- curl_easy_cleanup(curl);
- }
- return res;
- }
复制代码
|
-
-
e1.rar
1.38 MB, 下载次数: 127, 下载积分: 精币 -2 枚
|