C/C++ LibCurl 庫實現HTTP請求


1.下載libcurl https://curl.haxx.se/download.html

2.使用vs命令行控制台,切換到 cd curl-7.70.0\winbuild

3.執行編譯選項,兩種方式均可。

nmake /f Makefile.vc mode=dll VC=13 MACHINE=x86 DEBUG=no
nmake /f Makefile.vc mode=static VC=13 MACHINE=x86 DEBUG=no

4.測試curl是否可用

#define CURL_STATICLIB
#include <stdio.h>
#include "curl/curl.h"

#pragma comment ( lib, "libcurl.lib" )

int main(void)
{
	CURL *curl;
	CURLcode res;
	curl = curl_easy_init();
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_URL,"http://www.baidu.com");
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
	}

	printf("%d \n", curl);

	system("pause");
	return 0;
}

.初始化,獲取http請求頭
CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... );

#include <stdio.h>
#include "curl/curl.h"
#pragma comment ( lib, "libcurl.lib" )

int main(void)
{
	CURLcode return_code;
	return_code = curl_global_init(CURL_GLOBAL_WIN32);
	printf("初始化狀態: %d \n", return_code);
	if (CURLE_OK != return_code)
		return -1;

	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, "https://cn.bing.com"); // 請求的網站
		return_code = curl_easy_perform(easy_handle);                      // 執行CURL

		long retcode = 0;
		return_code = curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &retcode);
		if ((CURLE_OK == return_code) && retcode)
			printf("請求狀態碼: %d \n", retcode);


		char *ipAddress = { 0 };
		return_code = curl_easy_getinfo(easy_handle, CURLINFO_PRIMARY_IP, &ipAddress);
		if ((CURLE_OK == return_code) && ipAddress)
			printf("目標IP地址: %s \n", ipAddress);

		char *contentType = { 0 };
		return_code = curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_TYPE, &contentType);
		if ((CURLE_OK == return_code) && contentType)
			printf("請求的文件類型: %s \n", contentType);


		long requestSize = 0;
		return_code = curl_easy_getinfo(easy_handle, CURLINFO_REQUEST_SIZE, &requestSize);
		if ((CURLE_OK == return_code) && requestSize)
			printf("請求頭大小: %d \n", requestSize);

		long headerSize = 0;
		return_code = curl_easy_getinfo(easy_handle, CURLINFO_HEADER_SIZE, &headerSize);
		if ((CURLE_OK == return_code) && headerSize)
			printf("響應頭大小: %d \n", headerSize);
	}

	curl_easy_cleanup(easy_handle);
	curl_global_cleanup();

	system("pause");
	return 0;
}

libcurl 獲取源代碼並下載

#include <stdio.h>
#include "curl/curl.h"
#pragma comment ( lib, "libcurl.lib" )

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);

	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.baidu.com", "c://baidu.html");

	system("pause");
	return 0;
}

libCURL 獲取網頁請求:

#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);                       // 執行CURL

		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;
}

LibCURL發送POST數據:

#include <stdio.h>
#include "curl/curl.h"
#pragma comment ( lib, "libcurl.lib" )

bool Post(char *Url,char *Cookie,char *PostVal)
{
	CURL *curl;
	CURLcode res;

	curl = curl_easy_init();
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_URL,Url);                        // 指定url
		curl_easy_setopt(curl, CURLOPT_COOKIEFILE, Cookie);             // 指定cookie文件
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, PostVal);            // 指定post內容
		// 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[])
{
	Post("https://www.baidu.com/post.php", "exfffffx", "&logintype=uid&u=xieyan&psw=xxx86");
	system("pause");
	return 0;
}

libCURL 獲取頁面源碼並下載:

// ENABLE_IDN=no
#include <stdio.h>
#include "curl/curl.h"
#pragma comment ( lib, "libcurl.lib" )

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);
	curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);       // 在屏幕打印請求連接過程和返回http數據
	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.baidu.com", "c://baidu.html");

	system("pause");
	return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM