0
点赞
收藏
分享

微信扫一扫

[C++][原创]ubuntu上C++发送http请求get和post


找到一个开源项目:

GitHub - elnormous/HTTPRequest: Single-header C++ HTTP request class

使用项目都有介绍,很简单,这里我在ubuntu上使用CMakeLists跑起来

CMakeList.txt

cmake_minimum_required (VERSION 3.10)
project(test)
add_definitions(-std=c++11)
add_executable(main main.cpp HTTPRequest.hpp)
target_link_libraries(main pthread)

main.cpp

#include<iostream>

#include "HTTPRequest.hpp"

using namespace std;


int main()
{
  try
{
    // you can pass http::InternetProtocol::V6 to Request to make an IPv6 request
    http::Request request{"http://www.baidu.com"};

    // send a get request
    const auto response = request.send("GET");
    std::cout << std::string{response.body.begin(), response.body.end()} << '\n'; // print the result
}
catch (const std::exception& e)
{
    std::cerr << "Request failed, error: " << e.what() << '\n';
}
   
}

目录结构:

[C++][原创]ubuntu上C++发送http请求get和post_开发语言

这个库有个缺点:不支持https 

举报

相关推荐

0 条评论