Poco SendHttpRequest PocoServer 2021-03-31

2023-12-18 21:28

本文主要是介绍Poco SendHttpRequest PocoServer 2021-03-31,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

PocoServer

// PocoHttpServer.cpp : 定义控制台应用程序的入口点。
//#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;// ANSI ==> UTF8
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;
}/ Poco的HTTP服务端 /
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> &)		//run函数里面调用这个mian()函数	{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

// PocoHttpClient.cpp : 定义控制台应用程序的入口点。
//#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;// 获取当前exe所在目录,不包括最后的"\"
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()
{/ Poco的HTTP客户端 //try{//std::string sInputXml("发送的内容。。。");//std::string sUrl("http://127.0.0.1:9090");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);//		Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST);req.setChunkedTransferEncoding(false);req.setContentType("application/xml");//application/jsonreq.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){//CString strError;//strError.Format(_T("超时连接服务异常。Url:%s\r\n异常信息:%s"), strUrl, CString(CStringA(e.what())));//AfxMessageBox(strError, MB_ICONERROR);std::string s = e.what();s += " : " + e.message();}catch (Poco::Exception e){//strError.Format(_T("连接服务异常。Url:%s\r\n异常信息:%s"), strUrl, CString(CStringA(e.what())));//AfxMessageBox(strError, MB_ICONERROR);std::string s = e.what();s += " : " + e.message();}return FALSE;
}bool IsPathExist(const CString& strPath)
{return (-1 != _taccess(strPath, 0));
}// 获取当前exe所在目录,不包括最后的"\"
CString GetExeDir()
{//extern int __argc;          /* count of cmd line args *///extern char ** __argv;      /* pointer to table of cmd line args *///extern wchar_t ** __wargv;  /* pointer to table of wide cmd line args */TCHAR exeDir[_MAX_PATH] = { 0 };
#ifdef _UNICODE_tcscpy(exeDir, __wargv[0]);
#else_tcscpy(exeDir, __argv[0]);
#endif_tcsrchr(exeDir, '\\')[0] = '\0';return CString(exeDir);
}

这篇关于Poco SendHttpRequest PocoServer 2021-03-31的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/509808

相关文章

图形编辑器基于Paper.js教程03:认识Paper.js中的所有类

先来认一下Paper的资源对象,小弟有哪些,有个整体的认识。认个脸。 在Paper.js的 官方文档中类大致有如下这些: 基类: ProjectViewItemPointToolSizeSegmentRectangleCurveCurveLocationMatrixColorStyleTweenToolEventGradientGradientStopEvent 二级或三级类 继承Ite

Android自定义View学习笔记03

Android自定义View学习笔记03 参考gitHub上面的开源项目CircleImageView 预备知识 BitMap类 BitMap位图类,其中有一个嵌套类叫Bitmap.Config,内部有四个枚举值。这个类的作用是定义位图存储质量,即存储一个像素的位数,以及是否能显示透明、半透明颜色(Possible bitmap configurations. A bitmap co

软考初级网络管理员_03_硬件单选题

1.CPU是一块超大规模的集成电路,其主要部件有()。 运算器、控制器和系统总线 运算器、寄存器组和内存储器 控制器、存储器和寄存器组 运算器、控制器和寄存器组 2.(请作答此空)是指CPU一次可以处理的二进制的位数,它直接关系到计算机的计算精度、速度等指标:运算速度是指计算机每秒能执行的指令条数,通常以()为单位来描述。 宽带 主频 字长 存储容量 3.CPU执行指令时,先根

2021-02-16物料档案条码添加和蓝牙条码标签打印,金蝶安卓盘点机PDA,金蝶仓库条码管理WMS系统

物料档案条码添加和蓝牙条码标签打印,金蝶安卓盘点机PDA https://member.bilibili.com/platform/upload-manager/article 本期视频我们来讲解一下汉点机PDA条码添加和条码标签蓝牙便携打印: 在实际使用中,我们商品有两种情况: 一种是商品本身就有条码, 比如:超市卖的可口可乐,牛奶等商品,商品本身就有69开头的国标码,那么我们就可以使用盘点

03 TensorFlow 2.0:TOPK Accuracy实战

这江山风雨 岁月山河 刀光剑影 美了多少世间传说 且看他口若悬河 衣上有风尘 却原来是一位江湖说书人                                                                                                                                 《说书人》 在分类问题中会遇到TO

Day 31:100334. 包含所有1的最小矩形面积Ⅰ

Leetcode 100334. 包含所有1的最小矩形面积Ⅰ 给你一个二维 **二进制 **数组 grid。请你找出一个边在水平方向和竖直方向上、面积 最小 的矩形,并且满足 grid 中所有的 1 都在矩形的内部。 返回这个矩形可能的 **最小 **面积。 确定首次出现 1 的第一行 top,最后一次出现 1 的最后一列 r,最后一次出现 1 的最后一行 bottom,首次出现的第

Flink-03 Flink Java 3分钟上手 Stream 给 Flink-02 DataStreamSource Socket写一个测试的工具!

代码仓库 会同步代码到 GitHub https://github.com/turbo-duck/flink-demo 当前章节 继续上一节的内容:https://blog.csdn.net/w776341482/article/details/139875037 上一节中,我们需要使用 nc 或者 telnet 等工具来模拟 Socket 流。这节我们写一个 ServerSocket

第13关:存储过程1、第14关:存储过程2。(2021数据库期末一)

目录 首先需要学习和了解的知识 第13关:存储过程1 任务描述 答案  第14关:存储过程2 任务描述 答案 本篇博客的答案博主是学习别人得来的,敢于借鉴和学习哈哈!! 首先需要学习和了解的知识 了解什么是存储过程以及存储过程的基本语法。(作者博客专栏或者b站学习)了解在命令行中,执行创建存储过程的SQL时。需要通过关键字 delimiter 指定SQL语句的结束

source配置文件不生效 原创 2016年03月14日 18:43:55 3558 问题背景: 升级jdk 1.8之后,启动时报版本编译问题,查看$JAVA_HOME,$JRE_HOME

source配置文件不生效 原创  2016年03月14日 18:43:55 3558 问题背景:       升级jdk 1.8之后,启动时报版本编译问题,查看$JAVA_HOME,$JRE_HOME,没有问题。      初步推断是没有source,sourec .bashrc 之后查看$JAVA_HOME,$JRE_HOME变成1.8版本,但启动时还是报错,这就

Java基础的重点知识-03-方法与数组

文章目录 方法数组 方法 定义方法的格式详解 修饰符 返回值类型 方法名(参数列表){//代码省略...return 结果;} 修饰符: public static 固定写法返回值类型: 表示方法运行的结果的数据类型,方法执行后将结果返回到调用者参数列表:方法在运算过程中的未知数据,调用者调用方法时传递return:将方法执行后的结果带给调用者,方法执行到 return