gpt4 book ai didi

c++ - 使用 boost::asio 发送 http GET 请求,类似于 cURL

转载 作者:太空狗 更新时间:2023-10-29 19:52:15 34 4
gpt4 key购买 nike

我正在尝试使用某个域的 REST API 发送 http GET 请求。基本上我要做的是替换以下 curl 请求:

    curl -k  -H "Content-Type: application/json" -X GET 
--data '{"username":"user@name.co", "password":"test"}' https:/domain.name/api/login/

一些使用 boost::asio 的 C++ 代码。我不想在这里找到所有的 C++ 代码,但一些检查点会很棒。

最佳答案

我发现自己也在使用 boost 来发送一些“定制的”HTTP GET 请求——我在我的 Ubuntu 机器 (16.04) 上本地运行服务器。

在我的例子中,请求是我的服务器实现的一些专有 API(与它保存在数据库中的单词相关),但是你可以修改 queryStr 变量来保存你想要的任何查询字符串.

此外,在运行程序时更改 argv[1]argv[2] 以保存所需的值(IP 地址、查询和端口(如果需要) - 默认为 80)。

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;
using namespace std;

int main(int argc, char* argv[])
{
cout << "main -start" << endl;
try
{
boost::asio::io_service io_service;
string ipAddress = argv[1]; //"localhost" for loop back or ip address otherwise, i.e.- www.boost.org;
string portNum = argv[2]; //"8000" for instance;
string hostAddress;
if (portNum.compare("80") != 0) // add the ":" only if the port number is not 80 (proprietary port number).
{
hostAddress = ipAddress + ":" + portNum;
}
else
{
hostAddress = ipAddress;
}
string wordToQuery = "aha";
string queryStr = argv[3]; //"/api/v1/similar?word=" + wordToQuery;

// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(ipAddress, portNum);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

// Try each endpoint until we successfully establish a connection.
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);

// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "GET " << queryStr << " HTTP/1.1\r\n"; // note that you can change it if you wish to HTTP/1.0
request_stream << "Host: " << hostAddress << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";

// Send the request.
boost::asio::write(socket, request);

// Read the response status line. The response streambuf will automatically
// grow to accommodate the entire line. The growth may be limited by passing
// a maximum size to the streambuf constructor.
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");

// Check that response is OK.
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
std::cout << "Invalid response\n";
return 1;
}
if (status_code != 200)
{
std::cout << "Response returned with status code " << status_code << "\n";
return 1;
}

// Read the response headers, which are terminated by a blank line.
boost::asio::read_until(socket, response, "\r\n\r\n");

// Process the response headers.
std::string header;
while (std::getline(response_stream, header) && header != "\r")
{
std::cout << header << "\n";
}

std::cout << "\n";

// Write whatever content we already have to output.
if (response.size() > 0)
{
std::cout << &response;
}

// Read until EOF, writing data to output as we go.
boost::system::error_code error;
while (boost::asio::read(socket, response,boost::asio::transfer_at_least(1), error))
{
std::cout << &response;
}

if (error != boost::asio::error::eof)
{
throw boost::system::system_error(error);
}
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << "\n";
}

return 0;
}

我遵循的原始示例在这里:Boost-simple-http-get-request-sync-client

关于c++ - 使用 boost::asio 发送 http GET 请求,类似于 cURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28728347/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com