Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Concat boost::asio::read()响应_C++_Concatenation_Boost Asio - Fatal编程技术网

C++ Concat boost::asio::read()响应

C++ Concat boost::asio::read()响应,c++,concatenation,boost-asio,C++,Concatenation,Boost Asio,只是一个简短的问题。。。我使用sync_client.cpp(boost.asio给出的一个示例)来尝试这个库。 代码: #包括 #包括 #包括 #包括 #包括 #包括 使用boost::asio::ip::tcp; int main(int argc,char*argv[]) { 尝试 { boost::asio::io_服务io_服务; //获取与服务器名称对应的端点列表。 tcp::解析器解析器(io_服务); tcp::resolver::query查询(argv[1],“http”);

只是一个简短的问题。。。我使用sync_client.cpp(boost.asio给出的一个示例)来尝试这个库。 代码:

#包括
#包括
#包括
#包括
#包括
#包括
使用boost::asio::ip::tcp;
int main(int argc,char*argv[])
{
尝试
{
boost::asio::io_服务io_服务;
//获取与服务器名称对应的端点列表。
tcp::解析器解析器(io_服务);
tcp::resolver::query查询(argv[1],“http”);
tcp::resolver::iterator endpoint_iterator=resolver.resolve(查询);
//尝试每个端点,直到成功建立连接。
tcp::套接字(io_服务);
boost::asio::connect(套接字、端点迭代器);
//我们指定“Connection:close”头,以便
//服务器将在传输响应后关闭套接字。这将
//允许我们将EOF之前的所有数据视为内容。
boost::asio::streambuf请求;
std::ostream请求\u流(&request);

请求\u流不要忘记,您随时可以通过按底部的链接回答您自己的问题
回答您自己的问题
请为您的问题添加答案
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <string.h>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
try
{

boost::asio::io_service io_service;

// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(argv[1], "http");
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 " << argv[2] << " HTTP/1.0\r\n";
request_stream << "Host: " << argv[1] << "\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;
}
std::stringstream s;

while (boost::asio::read(socket, response,
      boost::asio::transfer_at_least(1), error))
s << &response;

const std::string htmlContent = s.str();