#include <stdio.h> #include "curl/curl.h" #pragma comment ( lib, "libcurl.lib" )
static size_t write_data(char *d, size_t n, size_t l, void *p) { return 0; }
void GetStatus(char *Host) { 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.baidu.com");
CURL *easy_handle = curl_easy_init(); if (NULL != easy_handle) { curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(easy_handle, CURLOPT_URL, Host); curl_easy_setopt(easy_handle, CURLOPT_WRITEFUNCTION, write_data); return_code = curl_easy_perform(easy_handle);
char *ipAddress = { 0 }; return_code = curl_easy_getinfo(easy_handle, CURLINFO_PRIMARY_IP, &ipAddress); if ((CURLE_OK == return_code) && ipAddress) printf("目标IP: %13s --> ", ipAddress);
long requestSize = 0; return_code = curl_easy_getinfo(easy_handle, CURLINFO_REQUEST_SIZE, &requestSize); if ((CURLE_OK == return_code) && requestSize) printf("请求头: %5d --> ", requestSize);
long headerSize = 0; return_code = curl_easy_getinfo(easy_handle, CURLINFO_HEADER_SIZE, &headerSize); if ((CURLE_OK == return_code) && headerSize) printf("响应头: %5d --> ", headerSize);
long retcode = 0; return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &retcode); if ((CURLE_OK == return_code) && retcode) printf("状态码: %5d \n", retcode); } curl_easy_cleanup(easy_handle); curl_global_cleanup(); }
int main(int argc, char *argv[]) { GetStatus("https://www.baidu.com"); system("pause"); return 0; }
|