Advertisement
Guest User

boost:asio:read

a guest
Jan 17th, 2014
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. int ServerRequest::Send()
  2. {
  3.     try
  4.     {
  5.         boost::asio::io_service io_service;
  6.  
  7.         // Get a list of endpoints corresponding to the server name.
  8.         tcp::resolver resolver(io_service);
  9.         tcp::resolver::query query(m_server_ip, m_server_port); // temp
  10.         tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  11.  
  12.         // Try each endpoint until we successfully establish a connection.
  13.         tcp::socket socket(io_service);
  14.         boost::asio::connect(socket, endpoint_iterator);
  15.         socket.set_option(tcp::no_delay(true));
  16.  
  17.         // Form the request. We specify the "Connection: close" header so that the
  18.         // server will close the socket after transmitting the response. This will
  19.         // allow us to treat all data up until the EOF as the content.
  20.         std::string json_body = GetBody();
  21.         WriteLog("Sending server request with data:" + json_body);
  22.         boost::asio::streambuf request;
  23.         std::ostream request_stream(&request);
  24.         request_stream << "POST " << "http://" << m_server_ip << ":" << m_server_port << "/" << m_server_path << " HTTP/1.1\r\n";
  25.         request_stream << "Host: " << m_server_ip << ":" << m_server_port << "\r\n";
  26.         request_stream << "Accept: */*\r\n";
  27.         request_stream << "Content-Type: application/json\r\n";
  28.         request_stream << "Content-Length: " << json_body.size() << "\r\n\r\n";
  29.         request_stream << json_body;
  30.  
  31.         // Send the request.
  32.         boost::asio::write(socket, request);
  33.  
  34.         // Read the response status line. The response streambuf will automatically
  35.         // grow to accommodate the entire line. The growth may be limited by passing
  36.         // a maximum size to the streambuf constructor.
  37.         boost::asio::streambuf response;
  38.         boost::asio::read_until(socket, response, "\r\n");
  39.  
  40.         // Check that response is OK.
  41.         std::istream response_stream(&response);
  42.         std::string http_version;
  43.         response_stream >> http_version;
  44.         unsigned int status_code;
  45.         response_stream >> status_code;
  46.         std::string status_message;
  47.         std::getline(response_stream, status_message);
  48.         if (!response_stream || http_version.substr(0, 5) != "HTTP/")
  49.         {
  50.             WriteLog("Invalid response");
  51.             return 1;
  52.         }
  53.         if (status_code != 200)
  54.         {
  55.             WriteLog("Response returned with status code " + status_code);
  56.             return 1;
  57.         }
  58.  
  59.         // Read the response headers, which are terminated by a blank line.
  60.         boost::asio::read_until(socket, response, "\r\n\r\n");
  61.  
  62.         // Process the response headers.
  63.         std::string header;
  64.         while (std::getline(response_stream, header) && header != "\r");
  65.  
  66.         // Read until EOF, writing data to output as we go.
  67.         std::stringstream response_xml_stream;
  68.         boost::system::error_code error;
  69.         while (boost::asio::read(socket, response,
  70.             boost::asio::transfer_at_least(1), error))
  71.         response_xml_stream << &response;
  72.         if (error != boost::asio::error::eof)
  73.             throw boost::system::system_error(error);
  74.  
  75.         m_response_data = ParseResponse(response_xml_stream);
  76.     }
  77.     catch (std::exception& e)
  78.     {
  79.         WriteLog("Exception:");
  80.         WriteLog(e.what());
  81.         return 1;
  82.   }
  83.  
  84.   return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement