#define CURL_STATICLIB #define BUILDING_LIBCURL #include <iostream> #include "curl/curl.h" #pragma comment (lib,"libcurl_a.lib") #pragma comment (lib,"wldap32.lib") #pragma comment (lib,"ws2_32.lib") #pragma comment (lib,"Crypt32.lib") using namespace std; int main(int argc, char *argv[]) { CURL *curl; CURLcode res; curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://www.lyshark.com"); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } std::cout << "返回状态: " << res << std::endl; system("pause"); return 0; }运行上述代码,读者可看到网站www.lyshark.com的源代码,如下图所示;
常量名称 | 描述 |
---|---|
CURLINFO_EFFECTIVE_URL | 最后一个有效的URL地址 |
CURLINFO_HTTP_CODE | 最后一个收到的HTTP代码 |
CURLINFO_FILETIME | 远程获取文档的时间,如果无法获取,则返回值为-1 |
CURLINFO_TOTAL_TIME | 最后一次传输所消耗的时间 |
CURLINFO_NAMELOOKUP_TIME | 名称解析所消耗的时间 |
CURLINFO_CONNECT_TIME | 建立连接所消耗的时间 |
CURLINFO_PRETRANSFER_TIME | 从建立连接到准备传输所使用的时间 |
CURLINFO_STARTTRANSFER_TIME | 从建立连接到传输开始所使用的时间 |
CURLINFO_REDIRECT_TIME | 在事务传输开始前重定向所使用的时间 |
CURLINFO_SIZE_UPLOAD | 以字节为单位返回上传数据量的总值 |
CURLINFO_SIZE_DOWNLOAD | 以字节为单位返回下载数据量的总值 |
CURLINFO_SPEED_DOWNLOAD | 平均下载速度 |
CURLINFO_SPEED_UPLOAD | 平均上传速度 |
CURLINFO_HEADER_SIZE | header部分的大小 |
CURLINFO_HEADER_OUT | 发送请求的字符串 |
CURLINFO_REQUEST_SIZE | 在HTTP请求中有问题的请求的大小 |
CURLINFO_SSL_VERIFYRESULT | 通过设置CURLOPT_SSL_VERIFYPEER返回的SSL证书验证请求的结果 |
CURLINFO_CONTENT_LENGTH_DOWNLOAD | 从Content-Length: field中读取的下载内容长度 |
CURLINFO_CONTENT_LENGTH_UPLOAD | 上传内容大小的说明 |
CURLINFO_CONTENT_TYPE | 下载内容的Content-Type:值,NULL表示服务器没有发送有效的Content-Type:header |
#define CURL_STATICLIB # define BUILDING_LIBCURL # include < iostream > #include "curl/curl.h" #pragma comment(lib, "libcurl_a.lib") # pragma comment(lib, "wldap32.lib")# pragma comment(lib, "ws2_32.lib") # pragma comment(lib, "Crypt32.lib") using namespace std; // 堆代码 duidaima.com // 设置CURLOPT_WRITEFUNCTION回调函数,返回为空屏蔽输出 static size_t write_data(char * d, size_t n, size_t l, void * p) { return 0; } // 获取网站返回值 void GetStatus(char * UrlPage) { CURLcode return_code; // 初始化模块 return_code = curl_global_init(CURL_GLOBAL_WIN32); if (CURLE_OK != return_code) { return; } // 初始化填充请求头 struct curl_slist * headers = NULL; headers = curl_slist_append(headers, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0)"); headers = curl_slist_append(headers, "Referer: https://www.lyshark.com"); // 初始化请求库 CURL * easy_handle = curl_easy_init(); if (NULL != easy_handle) { // CURLOPT_HTTPHEADER 自定义设置请求头 curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers); // CURLOPT_URL 自定义请求的网站 curl_easy_setopt(easy_handle, CURLOPT_URL, UrlPage); // CURLOPT_WRITEFUNCTION 设置回调函数,屏蔽输出 curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data); // 执行CURL访问网站 return_code = curl_easy_perform(easy_handle); char * ipAddress = { 0 }; // CURLINFO_PRIMARY_IP 获取目标IP信息 return_code = curl_easy_getinfo(easy_handle, CURLINFO_PRIMARY_IP, & ipAddress); if ((CURLE_OK == return_code) && ipAddress) { std::cout << "目标IP: " << ipAddress << std::endl; } long retcode = 0; // CURLINFO_RESPONSE_CODE 获取目标返回状态 return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, & retcode); if ((CURLE_OK == return_code) && retcode) { std::cout << "返回状态码: " << retcode << std::endl; } } curl_easy_cleanup(easy_handle); curl_global_cleanup(); } int main(int argc, char * argv[]) { GetStatus("https://www.lyshark.com"); system("pause"); return 0; }运行上述代码,则可以获取到www.lyshark.com目标主机的IP地址以及页面返回状态,如下图所示;
#define CURL_STATICLIB #define BUILDING_LIBCURL #include <iostream> #include "curl/curl.h" #pragma comment (lib,"libcurl_a.lib") #pragma comment (lib,"wldap32.lib") #pragma comment (lib,"ws2_32.lib") #pragma comment (lib,"Crypt32.lib") using namespace std; bool SendPost(char * Url, char * Cookie, char * PostVal) { CURL * curl; CURLcode res; // 初始化库 curl = curl_easy_init(); if (curl) { // 设置请求头 struct curl_slist * headers = NULL; headers = curl_slist_append(headers, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0)"); headers = curl_slist_append(headers, "Referer: https://www.lyshark.com"); // 设置请求头 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 指定URL curl_easy_setopt(curl, CURLOPT_URL, Url); // 指定cookie参数 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, Cookie); // 指定post内容 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, PostVal); // 是否代理 // curl_easy_setopt(curl, CURLOPT_PROXY, "10.99.60.201:8080"); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } return true; } int main(int argc, char * argv[]) { // 传入网址 cookie 以及post参数 SendPost("https://www.lyshark.com/post.php", "1e12sde342r2", "&logintype=uid&u=xieyan&psw=xxx86"); system("pause"); return 0; }该函数的调用需要有一个POST结构才可测试,此处由于我并没有指定接口所有返回了页面错误信息,如下图所示;
#define CURL_STATICLIB #define BUILDING_LIBCURL #include <iostream> #include "curl/curl.h" #pragma comment (lib,"libcurl_a.lib") #pragma comment (lib,"wldap32.lib") #pragma comment (lib,"ws2_32.lib") #pragma comment (lib,"Crypt32.lib") using namespace std; FILE * fp; size_t write_data(void * ptr, size_t size, size_t nmemb, void * stream) { int written = fwrite(ptr, size, nmemb, (FILE * ) fp); return written; } BOOL GetUrl(char * URL, char * FileName) { CURL * curl; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, URL); // 在屏幕打印请求连接过程和返回http数据 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1 L); // 查找次数,防止查找太深 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1); // 设置连接超时 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // 接收数据时超时设置 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); if ((fp = fopen(FileName, "w")) == NULL) { curl_easy_cleanup(curl); return FALSE; } // CURLOPT_WRITEFUNCTION 将后继的动作交给write_data函数处理 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_perform(curl); curl_easy_cleanup(curl); return TRUE; } int main(int argc, char * argv[]) { // 下载网页到本地 GetUrl("https://www.lyshark.com", "./lyshark.html"); system("pause"); return 0; }当读者运行上述程序后,即可将www.lyshark.com网站页面源码,下载到本地当前目录下lyshark.html,输出效果如下图所示;
#define CURL_STATICLIB #define BUILDING_LIBCURL #include <iostream> #include <string> #include "curl/curl.h" #pragma comment (lib,"libcurl_a.lib") #pragma comment (lib,"wldap32.lib") #pragma comment (lib,"ws2_32.lib") #pragma comment (lib,"Crypt32.lib") using namespace std; // 存储回调函数 size_t WriteCallback(char * contents, size_t size, size_t nmemb, void * userp) { ((std::string * ) userp) - > append((char * ) contents, size * nmemb); return size * nmemb; } // 获取数据并放入string中. std::string GetUrlPageOfString(std::string url) { std::string read_buffer; CURL * curl; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if (curl) { // 忽略证书检查 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0 L); // 重定向 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); // URL路径 curl_easy_setopt(curl, CURLOPT_URL, url); // 查找次数,防止查找太深 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1); // 连接超时 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // 接收数据时超时设置 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); // 写入回调函数 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, & read_buffer); curl_easy_perform(curl); curl_easy_cleanup(curl); return read_buffer; } return "None"; } int main(int argc, char * argv[]) { std::string urls = GetUrlPageOfString("https://www.lyshark.com"); std::cout << "接收长度: " << urls.length() << " bytes" << std::endl; system("pause"); return 0; }
如下图所示,则是运行后输出内存数据长度,当然我们也可以直接输出urls中的数据,也就是网页的源代码;