I want to create a simple HTTPClient with pion-net (Pion-net leverages boost::asio.).
Boot Version 1.49
Pion-net Version 4.11
My client should just be a able to:
- send a HTTP Request (this is working)
- receive the HTTP Response (not working)
- asynchronous code is not a must, synchronous would be ok
This is what I got:
#include <iostream>
#include "boost/asio.hpp"
#include "boost/thread.hpp"
#include "pion/net/HTTPRequestWriter.hpp"
#include "pion/net/HTTPResponseReader.hpp"
void FinishedResponseReading(pion::net::HTTPResponsePtr httpResponsePtr,
pion::net::TCPConnectionPtr tcpConnectionPtr,
const boost::system::error_code& errorCode_ref)
{
// ***************************
// this code is never reached!
// ***************************
std::cout << errorCode_ref << std::endl;
std::cout << httpResponsePtr->getContent() << std::endl;
tcpConnectionPtr->finish();
tcpConnectionPtr->close();
}
void FinishedRequestSending(const boost::system::error_code& error_code_ref,
pion::net::TCPConnectionPtr tcpConnectionPtr,
pion::net::HTTPRequest* httpRequest_ptr)
{
// ***************************
// this code is never reached!
// ***************************
pion::net::HTTPResponseReader::FinishedHandler fh =
boost::bind(FinishedResponseReading, _1, _2, _3);
pion::net::HTTPResponseReaderPtr httpResponseReaderPtr =
pion::net::HTTPResponseReader::create(tcpConnectionPtr,
*httpRequest_ptr,
fh);
httpResponseReaderPtr->receive();
}
int main()
{
boost::asio::io_service io_service;
// create and configure HTTPRequest
pion::net::HTTPRequest* httpRequest_ptr = new pion::net::HTTPRequest();
pion::net::HTTPRequestPtr httpRequestPtr(httpRequest_ptr);
httpRequest_ptr->setResource("/someService");
httpRequest_ptr->setMethod("PUT");
// create TCPConnection
pion::net::TCPConnection* tcpConnection_ptr =
new pion::net::TCPConnection(io_service);
pion::net::TCPConnectionPtr tcpConnectionPtr(tcpConnection_ptr);
// create HTTPRequestWriter
pion::net::HTTPRequestWriterPtr httpRequestWriterPtr(
pion::net::HTTPRequestWriter::create(tcpConnectionPtr,
httpRequestPtr,
boost::bind(FinishedRequestSending, _1,
tcpConnectionPtr,
httpRequest_ptr)));
// needed?
tcpConnection_ptr->setLifecycle(pion::net::TCPConnection::LIFECYCLE_KEEPALIVE);
// connect to server
tcpConnection_ptr->connect("192.168.1.14", 8080);
// send payload
httpRequestWriterPtr << "{\"someService\": \"something\"}";
httpRequestWriterPtr->send();
// ***********************************
// working fine so far! server is getting payload and is sending a HTTP Response
// but FinishedRequestSending is never reached
// ***********************************
// this is just to not exit immediately
boost::this_thread::sleep(boost::posix_time::milliseconds(15000));
// cleanup
delete(httpRequest_ptr);
delete(tcpConnection_ptr);
return 0;
}
If you want to communicate synchronously you could do something like this:
if however you need a little more sophisticated approach you could
pion::net::HTTPResponseReaderto wait asynch for a server response.header:
cpp: