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;
}
posted @ 2020-05-31 20:51  lyshark  阅读(3000)  评论(0编辑  收藏  举报

loading... | loading...
博客园 - 开发者的网上家园