0
点赞
收藏
分享

微信扫一扫

c++ http操作接口


很简单的使用libcurl来操作http与服务器来通讯,包含http与https,对外只开放

#include "request.h"
 #include "response.h"
 #include "url.h"

三个头文件,简单易用,使用的实例如下:

void test_get()
 {
     Request request("http://httpbin.org/ip");
     Response response = request.Perform();    printf("---------------------------------------------\n");
     printf("%s\n", to_string(response.status()));
     printf("---------------------------------------------\n");
     if (response.status() == Status::SUCCESS
         && response.http_status_code() == http::StatusCode::HTTP_200_OK)   // equal to response.ok()
     {
         // Header
         printf("------ Headers -------------------------------\n");
         auto& headers = response.headers();
         for (auto& header : headers) {
             printf("%s: %s\n", header.first.c_str(), header.second.c_str());
         }
         // Data
         printf("------ Body ----------------------------------\n");
         printf("%s\n", response.data().c_str());
         printf("---------------------------------------------\n");
     }
     printf("\n\n");
 }void test_url() 
 {
     Url url("https://api.icrystal.top/util/random");
     url.SetParam("min", "1");
     url.SetParam("max", "100");
     url.SetParam("count", "20");
     printf("GET: %s\n", url.ToString().c_str());    Request request(url.ToString());
     request.set_verify_ssl(false);
     auto response = request.Perform();    if (response.ok()) {
         // Header
         printf("------ Headers -------------------------------\n");
         for (auto& header : response.headers()) {
             printf("%s: %s\n", header.first.c_str(), header.second.c_str());
         }
         // Data
         printf("------ Body ----------------------------------\n");
         printf("%s\n", response.data().c_str());
         printf("---------------------------------------------\n");
     }
     else {
         printf("Request Error: %s %s\n", to_string(response.status()), http::to_string(response.http_status_code()));
     }
 }

举报

相关推荐

0 条评论