http server,基本就是官方源码中的例子:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <iostream>
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/TCPServer.h"
#include "Poco/Net/TCPServerConnectionFactory.h"
#include "Poco/Net/DNS.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/SocketStream.h"
#include "Poco/StreamCopier.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPServerResponse.h"
class RootHandler: public Poco::Net::HTTPRequestHandler
{
public:
void handleRequest(Poco::Net::HTTPServerRequest& request,
Poco::Net::HTTPServerResponse& response)
{
//
std::cout << request.getURI() << std::endl;
std::cout << request.getVersion() << std::endl;
std::cout << request.getContentType() << std::endl;
std::cout << request.getTransferEncoding() << std::endl;
std::string recv_string;
Poco::StreamCopier::copyToString(request.stream(), recv_string);
std::cout << recv_string << std::endl;
//
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr << "<html><head><title>HTTP Server powered by POCO C++ Libraries</title></head>";
ostr << "<body>";
ostr << "<h1>hello</h1>";
ostr << "</body></html>";
}
};
class MyRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory{
public:
MyRequestHandlerFactory(){};
~MyRequestHandlerFactory(){};
public:
virtual Poco::Net::HTTPRequestHandler* createRequestHandler(const Poco::Net::HTTPServerRequest& request)
{
return new RootHandler;
};
};
int main(int argc, char** argv)
{
Poco::UInt16 port = 1080;
Poco::Net::HTTPServerParams * pParams = new Poco::Net::HTTPServerParams;
pParams->setMaxQueued(10);
pParams->setMaxThreads(1);
Poco::Net::ServerSocket svs(port); // set-up a server socket
Poco::Net::HTTPServer srv(new MyRequestHandlerFactory(), svs, pParams);
// start the HTTPServer
srv.start();
//waitForTerminationRequest();
getchar();
// Stop the HTTPServer
srv.stop();
return 0;
}
http client,也是官方源码中的例子:
//
// httpget.cpp
//
// This sample demonstrates the HTTPClientSession and the HTTPCredentials classes.
//
// Copyright (c) 2005-2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include <Poco/Net/HTTPCredentials.h>
#include "Poco/StreamCopier.h"
#include "Poco/NullStream.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include "Poco/Net/HTMLForm.h"
#include <iostream>
bool doRequest(Poco::Net::HTTPClientSession& session, Poco::Net::HTTPRequest& request, Poco::Net::HTTPResponse& response)
{
session.sendRequest(request);
std::istream& rs = session.receiveResponse(response);
std::cout << response.getStatus() << " " << response.getReason() << std::endl;
if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_UNAUTHORIZED)
{
Poco::StreamCopier::copyStream(rs, std::cout);
return true;
}
else
{
Poco::NullOutputStream null;
Poco::StreamCopier::copyStream(rs, null);
return false;
}
}
int main(int argc, char** argv)
{
try
{
// json
std::string body("{key:value}");
// uri
//Poco::URI uri("http://www.baidu.com/test");
Poco::URI uri("http://127.0.0.1:1080/");
// session
Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
session.setKeepAlive(true);
// request
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, uri.getPathAndQuery(), Poco::Net::HTTPRequest::HTTP_1_1);
request.setContentType("application/json");
request.add("Accept", "Agent-007");
request.add("User-Agent", "xxxxoooo");
request.setContentLength(body.length());
//
session.sendRequest(request) << body;
// response
Poco::Net::HTTPResponse res;
std::string recv_string;
std::istream & is = session.receiveResponse(res);
int result = (int)res.getStatus();
std::cout << "result:" << result << ", reason:" << res.getReason() << std::endl;
Poco::StreamCopier::copyToString(is, recv_string);
std::cout << "recv : " << std::endl << recv_string << std::endl;
std::string recv_body = res.get("body");
std::cout << "body : " << std::endl << recv_body << std::endl;
}
catch (Poco::Exception& exc)
{
std::cerr << exc.displayText() << std::endl;
return 1;
}
return 0;
}