snikulov's picture

Hi,

Is it possible to implement http client using pion-network library?
Could you please provide short recipe for that usage?

Thank you.

Mike Dickey's picture
See WebServerTests.cpp

Most of our unit tests use the client API to test our server-side code. The best code we have right now is probably in WebServerTests.cpp (in the net/tests source directory).

There are two methods available for clients: synchronous and asynchronous. For synchronous, you just need to construct an HTTPRequest object, set the appropriate fields, and call HTTPRequest::send(). You can then create an empty HTTPResponse object and call receive() to get the response from the server. For asynchronous, you can use an HTTPRequestWriter class & specify a callback to use after the request has been delivered. Normally, the callback would be a function that uses an HTTPResponseReader to asynchronously parse the response.

I know.. Ideally we would have a tutorial for this, but unfortunately it just hasn't been a high-enough priority yet.. ;-)

Submitted by Mike Dickey on Fri, 11/21/2008 - 10:27.
eshober's picture
SSL Client Example

I'm trying to use the Pion net library (v2.0.10) to create an SSL client to connect to our Pion HTTP service. I'm having trouble with authentication. I've pieced together a code snippet below. The connection works but I get an authentication error. I can connect with Firefox so I know the HTTP service is operating properly and the username and password I'm using are the same. Can someone point out what I'm doing wrong? Thanks!

boost::shared_ptr context(
new pion::net::TCPConnection::SSLContext(scheduler.getIOService(),
boost::asio::ssl::context::sslv23));

TCPConnectionPtr tcp_conn(
new TCPConnection(scheduler.getIOService, *context);

boost::system::error_code error_code;
error_code = tcp_conn->connect(endpoint->get_host(), endpoint->get_port());
if (error_code || !tcp_conn->is_open())
{
std::cout << "Socket connect FAILED: " << error_code << std::endl;
}
else
{
std::cout << "Socket connect succeeded: " << error_code << std::endl;
error_code = tcp_conn->handshake_client();
if (error_code)
{
std::cout << "Handshake FAILED!" << std::endl;
}
}

HTTPRequest request(endpoint->get_url());
PionUserPtr user(new PionUser(username, password));

request.setUser(user);

request.setContentType(HTTPTypes::CONTENT_TYPE_TEXT);
request.setMethod("POST");

request.setVersionMajor(1);
request.setVersionMinor(1);

request.addHeader("Host", endpoint->get_host());

request.setContentLength(input_data_size);
char* content = request.createContentBuffer();

memcpy((void*)content, (void*)input_data.get(), input_data_size);

boost::system::error_code error_code;
request.send(*tcp_conn, error_code);

if (!error_code)
{
HTTPResponse response(request);
response.receive(*tcp_conn, error_code);
....

Submitted by eshober on Tue, 09/01/2009 - 08:59.
Mike Dickey's picture
For more source examples

For more source examples using pion-net for client-side HTTP, see this post: http://pion.org/node/200
 

Submitted by Mike Dickey on Mon, 04/05/2010 - 10:26.