Watch 1 Star 1 Fork 0

李惟其 / async_curlC/C++

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
使用libcurl实现的异步 http/https,支持常用的get/post请求 其它协议可自行添加 spread retract

Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

async_curl

介绍

使用libcurl实现的异步 http/https 其它协议可自行添加

软件架构

依赖libcurl至少7.66.0的版本,目前Windows和linux编译的是7.67.0的版本

安装教程

linux:

  1. 升级libcurl至少到7.66的版本

  2. cd到async_curl/build/linux目录下

  3. make编译生成libasync_curl.so库,make test编译生成测试文件

  4. 运行./testexe测试用例

windows:

  1. 可以直接使用git中上传的libcurl该版本为7.67.0以动态库链接的方式(c/c++用的md/mdd编译)

  2. 直接打开async_curl/build/windows下的项目工程文件

  3. 编译生成相关库文件,依赖的库在async_curl/libcurl/lib/win目录下

使用说明

  1. 引用头文件async_curl/include

  2. 引用库libasync_curl.so

  3. 测试用例sync_curl/testcpp/test.cpp


struct MyCallBack: public CurlCallBack { virtual int operator() (MsgPack* pack) { HttpMsgPack* http_pack = dynamic_cast<HttpMsgPack*>(pack);

	if (http_pack->_errMsg.empty())
	{
		// 输出到文件
		string filename = "out_" + to_string(pack->_msgid) + ".txt";
		fstream o(filename, ios::in | ios::out | ios::trunc);
		o << http_pack->_rspData;
		o.close();

		// 输出到屏幕
		cout << http_pack->_rspData;
	}
	else
	{
		cout << "error:" << http_pack->_errMsg;
	}

	return 0;
}

};

int main() { // 配置 HttpConfig config; config._timeout = 30;

// 服务
Server* server = Server::CreateHttp(WriteLogConsole);

// 初始化
server->Init(&config);

MyCallBack mycallback;
server->SetCallBack(&mycallback);

thread thr(std::bind(&Server::Run, server));

/*
	投递消息:
	默认投递get方法,目前不加任何头部字段信息;
	只加了一个Content-Type:application/x-www-form-urlencoded;charset=UTF-8,
	改头部信息由pack._format = HttpContextFormat::URLENCODED指定
 */
int numcount = 100;
while (--numcount > 0)
{
	HttpMsgPack pack;
	pack._format = HttpContextFormat::URLENCODED;
	pack._url = "www.baidu.com";
	server->PostPack(&pack);
	this_thread::sleep_for(chrono::seconds(2));
}

thr.join();

return 0;

}

Comments ( 0 )

Sign in for post a comment

C/C++
1
https://gitee.com/ganmaojiushijiu/async_curl.git
git@gitee.com:ganmaojiushijiu/async_curl.git
ganmaojiushijiu
async_curl
async_curl
master

Help Search