Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int ServerRequest::Send()
- {
- 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(m_server_ip, m_server_port); // temp
- 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);
- socket.set_option(tcp::no_delay(true));
- // 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.
- std::string json_body = GetBody();
- WriteLog("Sending server request with data:" + json_body);
- boost::asio::streambuf request;
- std::ostream request_stream(&request);
- request_stream << "POST " << "http://" << m_server_ip << ":" << m_server_port << "/" << m_server_path << " HTTP/1.1\r\n";
- request_stream << "Host: " << m_server_ip << ":" << m_server_port << "\r\n";
- request_stream << "Accept: */*\r\n";
- request_stream << "Content-Type: application/json\r\n";
- request_stream << "Content-Length: " << json_body.size() << "\r\n\r\n";
- request_stream << json_body;
- // 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/")
- {
- WriteLog("Invalid response");
- return 1;
- }
- if (status_code != 200)
- {
- WriteLog("Response returned with status code " + status_code);
- 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");
- // Read until EOF, writing data to output as we go.
- std::stringstream response_xml_stream;
- boost::system::error_code error;
- while (boost::asio::read(socket, response,
- boost::asio::transfer_at_least(1), error))
- response_xml_stream << &response;
- if (error != boost::asio::error::eof)
- throw boost::system::system_error(error);
- m_response_data = ParseResponse(response_xml_stream);
- }
- catch (std::exception& e)
- {
- WriteLog("Exception:");
- WriteLog(e.what());
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement