Haruyuki Mohri

毛利春幸のブログです。

Use the C++BuilderXE8(win64) http connection "boost::asio"

http connection using boost::asio

I used the previous TIdHTTP. So, we use the asio.

How to use "async" and "future<>" in XE8(Win64) - (Delphi, C++Builder)Haruyuki Mohri
C++Builder XE8(Win64) http connection "boost::asio" · GitHub

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    std::string url = "www.mojeld.com";
    std::future<std::string> fs;
    fs = std::async(std::launch::async, [url]()->std::string
    {
        using namespace boost::asio;
        io_service io_service1;

        ip::tcp::socket socket1(io_service1);
        ip::tcp::resolver resolver1(io_service1);

        ip::tcp::resolver::query q1(url, "http");
        ip::tcp::endpoint ep1(*resolver1.resolve(q1));

        socket1.connect(ep1);

        boost::asio::streambuf req;
        ostream reqout(&req);

        reqout << "GET / HTTP/1.0\r\n";
        reqout << "Host: " << url << "\r\n\r\n";

        write(socket1, req);

        boost::asio::streambuf res;
        boost::system::error_code err;
        read(socket1, res, transfer_all(), err);

        std::string textout = buffer_cast<const char*>(res.data());
        Sleep(1000);
        return textout;
        });
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
    fs.wait();
    Memo1->Lines->Append( fs.get().c_str() );

}
//---------------------------------------------------------------------------

It replaced the previous boost::asio
Unicode of the cast does not know in "buffer_cast<>".
Japanese is garbled.

f:id:mojeld:20150811110332p:plain