PocoServer
#include "stdafx.h"
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/Net/HTTPRequestHandlerFactory.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/StreamCopier.h>
#include <Poco/Util/ServerApplication.h>
using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPServerResponse;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPRequestHandlerFactory;
using namespace Poco::Net;
using Poco::Util::ServerApplication;
bool ANSI_to_UTF8(const std::string &sAnsi, std::string &sUtf8)
{
if (sAnsi.empty())
return true;
std::wstring wsAnsi;
int nLen = ::MultiByteToWideChar(CP_ACP, NULL, sAnsi.c_str(), -1, NULL, 0);
wchar_t *buf1 = new wchar_t[nLen];
int nWrited = ::MultiByteToWideChar(CP_ACP, NULL, sAnsi.c_str(), -1, buf1, nLen);
wsAnsi = buf1;
delete[] buf1;
if (nWrited != nLen)
return false;
nLen = ::WideCharToMultiByte(CP_UTF8, NULL, wsAnsi.c_str(), -1, NULL, 0, NULL, NULL);
char* buf2 = new char[nLen];
nWrited = ::WideCharToMultiByte(CP_UTF8, NULL, wsAnsi.c_str(), -1, buf2, nLen, NULL, NULL);
sUtf8 = buf2;
delete[] buf2;
return (nWrited == nLen) ? true : false;
}
class MyRequestHandler : public HTTPRequestHandler
{
public:
virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp)
{
std::string sUtf8;
ANSI_to_UTF8("成功", sUtf8);
std::istream& istr = req.stream();
std::string sRequest;
Poco::StreamCopier::copyToString(istr, sRequest);
resp.setStatus(HTTPResponse::HTTP_OK);
resp.setContentType("application/x-www-form-urlencoded\r\n");
ostream& out = resp.send();
out << "<h1>Hello world!</h1>" << "\r\n\r\n"
<< "<p>Count: " << ++count << "</p>" << "\r\n\r\n"
<< "<p>Host: " << req.getHost() << "</p>" << "\r\n\r\n"
<< "<p>Method: " << req.getMethod() << "</p>" << "\r\n\r\n"
<< "<p>URI: " << req.getURI() << "</p>" << "\r\n\r\n"
<< "<p>ContentRequest: " << sRequest << "</p>" << "\r\n\r\n"
<< sUtf8;
out.flush();
}
private:
static int count;
};
int MyRequestHandler::count = 0;
class MyRequestHandlerFactory : public HTTPRequestHandlerFactory
{
public:
virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest &)
{
return new MyRequestHandler;
}
};
class MyServerApp : public ServerApplication
{
protected:
int main(const vector<string> &)
{
HTTPServer s(new MyRequestHandlerFactory, ServerSocket(9090), new HTTPServerParams);
s.start();
cout << endl << "Server started" << endl;
waitForTerminationRequest();
cout << endl << "Shutting down..." << endl;
s.stop();
return Application::EXIT_OK;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
MyServerApp app;
return app.run(argc, argv);
}
PocoClient
#include "stdafx.h"
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/URI.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Util/PropertyFileConfiguration.h>
#include <Poco/AutoPtr.h>
using Poco::AutoPtr;
using Poco::Util::PropertyFileConfiguration;
CString GetExeDir();
bool IsPathExist(const CString& sPath);
BOOL SendHttpRequest();
int _tmain(int argc, _TCHAR* argv[])
{
CString str = GetExeDir();
bool b = IsPathExist(_T("E:\\111"));
`PocoServer`();
system("pause");
return 0;
}
BOOL SendHttpRequest()
{
try
{
AutoPtr<PropertyFileConfiguration> pfc = new PropertyFileConfiguration("Config/PocoHttpClient.properties");
std::string sInputXml = pfc->getString("PocoHttpClient.ContentSended");
std::string sUrl = pfc->getString("PocoHttpClient.Url");
Poco::URI uri(sUrl);
std::string path(uri.getPathAndQuery());
path = uri.getPath();
path = uri.getPathAndQuery();
path = sUrl;
Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
int nTimeout = 20;
Poco::Timespan pocoTimeSpan(nTimeout, 0);
session.setTimeout(pocoTimeSpan);
Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);
req.setChunkedTransferEncoding(false);
req.setContentType("application/xml");
req.setContentLength(sInputXml.length());
session.sendRequest(req) << sInputXml;
Poco::Net::HTTPResponse res;
std::istream &is = session.receiveResponse(res);
int nHttpStatus = res.getStatus();
if (Poco::Net::HTTPResponse::HTTPStatus::HTTP_OK != nHttpStatus)
return FALSE;
std::istreambuf_iterator<char> eos;
std::string sRes(std::istreambuf_iterator<char>(is), eos);
cout << sRes << endl;
return TRUE;
}
catch (Poco::TimeoutException e)
{
std::string s = e.what();
s += " : " + e.message();
}
catch (Poco::Exception e)
{
std::string s = e.what();
s += " : " + e.message();
}
return FALSE;
}
bool IsPathExist(const CString& strPath)
{
return (-1 != _taccess(strPath, 0));
}
CString GetExeDir()
{
TCHAR exeDir[_MAX_PATH] = { 0 };
#ifdef _UNICODE
_tcscpy(exeDir, __wargv[0]);
#else
_tcscpy(exeDir, __argv[0]);
#endif
_tcsrchr(exeDir, '\\')[0] = '\0';
return CString(exeDir);
}