Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:49:59+00:00 2026-06-18T06:49:59+00:00

I want to create a simple HTTPClient with pion-net (Pion-net leverages boost::asio.). Boot Version

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:50:00+00:00Added an answer on June 18, 2026 at 6:50 am

    If you want to communicate synchronously you could do something like this:

    int main()
    {
        boost::asio::io_service io_service
        pion::net::TCPConnection tcpConnection(io_service);
    
        pion::net::HTTPRequest httpRequest;
        httpRequest.setResource("/server/resource");
        httpRequest.setMethod("PUT");
        httpRequest.setMinor(1);
        httpRequest.setMajor(1);
        httpRequest.setContent("test");
    
        boost::system::error_code ec;
        ec = tcpConnection.connect("192.168.1.1", 80); // blocks till connected or timed out
        if (!ec)
        {
            httpRequest.send(tcpConnection, ec); // never blocks
            if (!ec)
            {
                pion::net::HTTPResponse(httpRequest);
                httpResponse.receive(tcpConnection, ec); // this might block forever :-(
                // httpResponse.receive seems to be IO dependent, you can set your socket to timeout
                if (!ec)
                {
                    httpResponse.write(std::cout, ec);
                }
            }
        }
    }
    

    if however you need a little more sophisticated approach you could pion::net::HTTPResponseReader to wait asynch for a server response.
    header:

    class MyHTTPClient {
    public:
        void close();
        pion::net::HTTPResponsePtr blockingReceiveOrTimeout(pion::net::HTTPRequest httpRequest, boost::system::error_code& ec_ref);
        MyHTTPClient(boost::asio::ip::address aServerIP, unsigned int aServerPort);
        virtual ~MyHTTPClient();
    private:
        boost::asio::ip::address            mp_serverIP;
        unsigned int                    mp_serverPort;
        boost::asio::io_service             mp_ioService;
        pion::net::TCPConnectionPtr         mp_tcpConnectionPtr;
        pion::net::HTTPResponsePtr          mp_curr_httpResponsePtr;
        boost::system::error_code           mp_curr_errorCode;
    
        void finishedReceiveResponse(pion::net::HTTPResponsePtr httpResponsePtr, const boost::system::error_code& error_code_ref);
    
    };
    

    cpp:

    MyHTTPClient::MyHTTPClient(boost::asio::ip::address aServerIP, unsigned int aServerPort) : mp_serverIP(aServerIP), mp_serverPort(aServerPort)
    {
        mp_tcpConnectionPtr.reset(new pion::net::TCPConnection(mp_ioService));
        mp_tcpConnectionPtr->setLifecycle(pion::net::TCPConnection::LIFECYCLE_KEEPALIVE);
    }
    
    MyHTTPClient::~MyHTTPClient()
    {
         mp_tcpConnectionPtr->close();
    }
    
    void MyHTTPClient::close()
    {
        mp_tcpConnectionPtr->close();
    }
    
    pion::net::HTTPResponsePtr MyHTTPClient::blockingReceiveOrTimeout(pion::net::HTTPRequest httpRequest, boost::system::error_code& error_code_ref)
    {
        // reinit
        mp_curr_httpResponsePtr.reset();
        mp_ioService.reset();
        error_code_ref.clear();
    
        // connect to server if not already connectec
        if (!mp_tcpConnectionPtr->is_open())
        {
            error_code_ref = mp_tcpConnectionPtr->connect(mp_serverIP, mp_serverPort);
        }
        if (!error_code_ref)
        {
            // send Request
            httpRequest.send(*mp_tcpConnectionPtr.get(), error_code_ref, false);
    
            if (!error_code_ref)
            {
                // asynchronously wait for response (times out automatically)
                pion::net::HTTPResponseReader::FinishedHandler responseReaderFinishHandler = boost::bind(&MyHTTPClient::finishedReceiveResponse, this, _1, _3);
                const pion::net::HTTPRequest constHTTPRequest = httpRequest;
                pion::net::HTTPResponseReaderPtr httpResponseReaderPtr = pion::net::HTTPResponseReader::create(
                        mp_tcpConnectionPtr,
                        constHTTPRequest,
                        responseReaderFinishHandler);
                 httpResponseReaderPtr->receive();
                 mp_ioService.run();
            }
        }
        return mp_curr_httpResponsePtr;
    }
    
    void MyHTTPClient::finishedReceiveResponse(pion::net::HTTPResponsePtr httpResponsePtr, const boost::system::error_code& error_code_ref)
    {
        mp_curr_httpResponsePtr = httpResponsePtr;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create simple app able to edit images. Main view of app
I want to create simple file viewer. What control should i use to view
I'm new to ASP.NET and C# and I want to create simple webapp. How
Just want to create a simple contact form in Symfony 2.1. How? ContactForm.php: namespace
i want create one simple windows form ,it just contain Editcontrol(textbox),Static Edit(label),Button (Name of
JavaScript. I want to create simple script, that will be resize loaded image using
I want to create a simple app, where the user can take a photo.
I want to create a simple app that shows me the city of the
I want to create a simple Javascript program with a HTML interface. The program
I want to create a simple server application which runs on the desktop, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.