Skip to content

基于 Boost.Asio 实现的 HTTP 客户端网络库

License

Notifications You must be signed in to change notification settings

senlinzhan/requests

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT Build status

requests

基于 Boost.Asio 实现的 HTTP 客户端网络库,主要特性:

  • 支持发送 GET 和 POST 请求
  • 支持同步和异步发送请求
  • 允许用户设置回调函数
  • URL 自动编码
  • 支持多线程

快速上手

同步发送请求

#include <requests/Exception.hpp>
#include <requests/Request.hpp>
#include <requests/Url.hpp>

#include <iostream>

int main()
{
    requests::Request request;
    requests::Url url("http://www.baidu.com");
    
    try
    {
        // 发起 HTTP 请求,阻塞
        auto resp = request.get(url);

        std::cout << resp.statusCode() << std::endl;               // 200
        std::cout << resp.headers()["Content-Type"] << std::endl;  // text/html
    }
    catch (requests::Exception &e)
    {
        std::cout << e.what() << std::endl;
    }	
		
    return 0;
}

异步发送请求

#include <requests/AsyncRequest.hpp>
#include <requests/Exception.hpp>
#include <requests/Response.hpp>
#include <requests/Url.hpp>

#include <iostream>

// 用户提供的回调函数
void callback(requests::Response &resp)
{
    std::cout << resp.statusCode() << std::endl;               // 200
    std::cout << resp.headers()["Content-Type"] << std::endl;  // text/html
}

// 错误处理函数
void errorCallback(const requests::Exception &e)
{
    std::cout << e.what() << std::endl;
}

int main()
{
    requests::AsyncRequest asyncRequest(4);                   // 启动 4 个线程 
    requests::Url url("http://www.baidu.com");
    
    // 发起 HTTP 请求,非阻塞
    asyncRequest.get(url, callback, errorCallback);
    
    return 0;
}

Releases

No releases published

Packages

No packages published