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

相关文章

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

FreeRTOS内部机制学习03(事件组内部机制)

文章目录 事件组使用的场景事件组的核心以及Set事件API做的事情事件组的特殊之处事件组为什么不关闭中断xEventGroupSetBitsFromISR内部是怎么做的? 事件组使用的场景 学校组织秋游,组长在等待: 张三:我到了 李四:我到了 王五:我到了 组长说:好,大家都到齐了,出发! 秋游回来第二天就要提交一篇心得报告,组长在焦急等待:张三、李四、王五谁先写好就交谁的

GPU 计算 CMPS224 2021 学习笔记 02

并行类型 (1)任务并行 (2)数据并行 CPU & GPU CPU和GPU拥有相互独立的内存空间,需要在两者之间相互传输数据。 (1)分配GPU内存 (2)将CPU上的数据复制到GPU上 (3)在GPU上对数据进行计算操作 (4)将计算结果从GPU复制到CPU上 (5)释放GPU内存 CUDA内存管理API (1)分配内存 cudaErro

Linux 删除 当前下的 mysql-8.0.31 空文件夹

在Linux中,如果你想要删除当前目录下的名为mysql-8.0.31的空文件夹(即该文件夹内没有任何文件或子文件夹),你可以使用rmdir命令。但是,如果mysql-8.0.31文件夹并非完全为空(即它包含文件或子文件夹),rmdir命令会失败。 如果你的目标是删除mysql-8.0.31文件夹及其内部的所有内容(无论是否为空),你应该使用rm命令结合-r(或-R,它们是等价的)选项来递归地删

Vue day-03

目录 Vue常用特性 一.响应更新 1. 1 v-for更新监测 1.2 v-for就地更新 1.3 什么是虚拟DOM 1.4 diff算法更新虚拟DOM 总结:key值的作用和注意点: 二.过滤器 2.1 vue过滤器-定义使用 2.2 vue过滤器-传参和多过滤器 三. 计算属性(computed) 3.1 计算属性-定义使用 3.2 计算属性-缓存 3.3 计算属

2021-8-14 react笔记-2 创建组件 基本用法

1、目录解析 public中的index.html为入口文件 src目录中文件很乱,先整理文件夹。 新建components 放组件 新建assets放资源   ->/images      ->/css 把乱的文件放进去  修改App.js 根组件和index.js入口文件中的引入路径 2、新建组件 在components文件夹中新建[Name].js文件 //组件名首字母大写

2021-08-14 react笔记-1 安装、环境搭建、创建项目

1、环境 1、安装nodejs 2.安装react脚手架工具 //  cnpm install -g create-react-app 全局安装 2、创建项目 create-react-app [项目名称] 3、运行项目 npm strat  //cd到项目文件夹    进入这个页面  代表运行成功  4、打包 npm run build

[SWPUCTF 2021 新生赛]web方向(一到六题) 解题思路,实操解析,解题软件使用,解题方法教程

题目来源 NSSCTF | 在线CTF平台因为热爱,所以长远!NSSCTF平台秉承着开放、自由、共享的精神,欢迎每一个CTFer使用。https://www.nssctf.cn/problem   [SWPUCTF 2021 新生赛]gift_F12 这个题目简单打开后是一个网页  我们一般按F12或者是右键查看源代码。接着我们点击ctrl+f后快速查找,根据题目给的格式我们搜索c

【SpringMVC学习03】-SpringMVC的配置文件详解

在SpringMVC的各个组件中,处理器映射器、处理器适配器、视图解析器称为springmvc的三大组件。其实真正需要程序员开发的就两大块:一个是Handler,一个是jsp。 在springMVC的入门程序中,SpringMVC的核心配置文件——springmvc.xml为: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http:

浙大数据结构——03-树1 树的同构

这道题我依然采用STL库的map,从而大幅减少了代码量 简单说一下思路,两棵树是否同构,只需比较俩树字母相同的结点是否同构,即是否左==左,右==右或者左==右,右==左。 1、条件准备 atree和btree是存两个数结点字母,第几个就存输入的第几个结点的字母。 map通过结点的字母作为键,从而找到两个子节点的信息 都要用char类型 #include <iostream>#inc