Skip to content

Instantly share code, notes, and snippets.

@xulman
Created March 24, 2021 16:08
Show Gist options
  • Save xulman/23f08764fe76cc6b95d70bce36fc3788 to your computer and use it in GitHub Desktop.
Save xulman/23f08764fe76cc6b95d70bce36fc3788 to your computer and use it in GitHub Desktop.
C++ example code how to submit http comm using the Poco project
$ more *.*
::::::::::::::
CMakeLists.txt
::::::::::::::
cmake_minimum_required (VERSION 3.5)
project (poco-VladoClientTest)
find_package (Poco REQUIRED Net)
add_executable (clientTest main.cpp)
target_link_libraries (clientTest Poco::Net)
::::::::::::::
main.cpp
::::::::::::::
#include <iostream>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/NetException.h>
int main(int argc,char** argv)
{
try {
using namespace Poco::Net;
HTTPClientSession session("www.seznam.cz",80);
std::cout << "is connected? " << session.connected() << "\n"; //not yet
HTTPRequest request(HTTPRequest::HTTP_GET, "/");
session.sendRequest(request);
//or, std::ostream &postDataGoesHere = session.sendRequest(request);
std::cout << "is connected? " << session.connected() << "\n"; //already is
HTTPResponse response;
session.receiveResponse( response );
std::cout << "got back: status = " << response.getStatus() << "\n";
std::cout << "content starts:\n";
response.write( std::cout );
std::cout << "content ends:\n";
}
catch (Poco::Net::NetException& e) {
std::cout << "Got exception: " << e.what() << "\n";
}
return 0;
}
@xulman
Copy link
Author

xulman commented Mar 24, 2021

here's how to communicate to SimViewer:

std::string msg("v1 points 2 dim 3 1 7.7 8.8 9.9 5 2 2 17.7 8.8 9.9 5 3");
std::string msG("v1 points 2 dim 3 333 7.7 8.8 19.9 5 2 444 17.7 8.8 19.9 5 3");

using namespace Poco::Net;

//HTTPClientSession session("httpbin.org",80); //btw, nice "echo server"
HTTPClientSession session("localhost",8765);
HTTPRequest request(HTTPRequest::HTTP_POST, "/simViewer"); //URI might change in the future

request.setContentLength(msg.size());
std::ostream &requestBody = session.sendRequest(request); //connects now!
requestBody << msg; //sends the data immediately!

request.setContentLength(msG.size());
std::ostream &requestBodyB = session.sendRequest(request); //connects now!
requestBodyB << msG; //sends the data immediately!

//hear back some acknowledgement:
HTTPResponse response;
std::istream &responseBody = session.receiveResponse(response);

if (response.getStatus() != HTTPResponse::HTTP_OK)
    std::cerr << "couldn't transfer the message!\n";
else
    std::cout << "message transferred!\n";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment