轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 终极进阶 查看主题

微信小程序lianc++后台

发布者: sonich | 发布时间: 2017-12-24 08:24| 查看数: 7652| 评论数: 1|帖子模式

本文作者:abqchina,来自原文地址

贴上微信小程序发送http请求代码:

[javascript] view plain copy
  1. onsend: function(){      
  2.    wx.request({  
  3.       url: '', //c++后台ip、端口  
  4.       data: {  
  5.           x: '1' , //发送到后台字段  
  6.           y: '2'  
  7.  },  
  8.  header:{  
  9.      "Content-Type":"application/json"  
  10.  },  
  11.  method:"POST"//发送POST,若为GET则改为GET  
[javascript] view plain copy
  1. success: function(res) {  
  2.    var data = res.data;  
  3.    console.log(data);  
  4. }  
  5. });  
  6. }  

c++后台代码借鉴boost官网的asio的http server 3地址并自己做了修改:

官网地址:

修改代码:

1、request_parser.hpp:

[cpp] view plain copy
  1. //  
  2. // request_parser.hpp  
  3. // ~~~~~~~~~~~~~~~~~~  
  4. //  
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)  
  6. //  
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying  
  8. // file LICENSE_1_0.txt or copy at   
  9. //  
  10.   
  11. #ifndef HTTP_SERVER3_REQUEST_PARSER_HPP  
  12. #define HTTP_SERVER3_REQUEST_PARSER_HPP  
  13.   
  14. #include <boost/logic/tribool.hpp>  
  15. #include <boost/tuple/tuple.hpp>  
  16.   
  17. namespace http {  
  18. namespace server3 {  
  19.   
  20. struct request;  
  21.   
  22. /// Parser for incoming requests.  
  23. class request_parser  
  24. {  
  25. public:  
  26.   /// Construct ready to parse the request method.  
  27.   request_parser();  
  28.   
  29.   /// Reset to initial parser state.  
  30.   void reset();  
  31.   
  32.   /// Parse some data. The tribool return value is true when a complete request  
  33.   /// has been parsed, false if the data is invalid, indeterminate when more  
  34.   /// data is required. The InputIterator return value indicates how much of the  
  35.   /// input has been consumed.  
  36.   template <typename InputIterator>  
  37.   boost::tuple<boost::tribool, InputIterator> parse(request& req,  
  38.       InputIterator begin, InputIterator end)  
  39.   {  
  40.     if(req.method=="POST") state_ = expecting_newline_4; //自己增加针对post请求数据一次性接收不完问题  
  41.     while (begin != end)  
  42.     {  
  43.       boost::tribool result = consume(req, *begin++);  
  44.         
  45.       if (state_ != none && (result || !result)){ //针对post请求做特殊处理  
  46.   
  47.            if(req.method=="POST"){  
  48.                char c = *begin;  
  49.                if(c=='\0'break;  
  50.              state_ = expecting_newline_4;  
  51.              result = consume(req, *begin++);  
  52.            }    
  53.            
  54.       }  
  55.       if (result || !result){  
  56.             return boost::make_tuple(result, begin);  
  57.        }  
  58.     }  
  59.     boost::tribool result = boost::indeterminate;  
  60.     return boost::make_tuple(result, begin);  
  61.   }  
  62.   
  63. private:  
  64.   /// Handle the next character of input.  
  65.   boost::tribool consume(request& req, char input);  
  66.   
  67.   /// Check if a byte is an HTTP character.  
  68.   static bool is_char(int c);  
  69.   
  70.   /// Check if a byte is an HTTP control character.  
  71.   static bool is_ctl(int c);  
  72.   
  73.   /// Check if a byte is defined as an HTTP tspecial character.  
  74.   static bool is_tspecial(int c);  
  75.   
  76.   /// Check if a byte is a digit.  
  77.   static bool is_digit(int c);  
  78.   
  79.   /// The current state of the parser.  
  80.   enum state  
  81.   {  
  82.     method_start,  
  83.     method,  
  84.     uri_start,  
  85.     uri,  
  86.     http_version_h,  
  87.     http_version_t_1,  
  88.     http_version_t_2,  
  89.     http_version_p,  
  90.     http_version_slash,  
  91.     http_version_major_start,  
  92.     http_version_major,  
  93.     http_version_minor_start,  
  94.     http_version_minor,  
  95.     expecting_newline_1,  
  96.     header_line_start,  
  97.     header_lws,  
  98.     header_name,  
  99.     space_before_header_value,  
  100.     header_value,  
  101.     expecting_newline_2,  
  102.     expecting_newline_3,  
  103.     expecting_newline_4,//针对post请求  
  104.     none //针对post请求  
  105.   } state_;  
  106. };  
  107.   
  108. // namespace server3  
  109. // namespace http  
  110.   
  111. #endif // HTTP_SERVER3_REQUEST_PARSER_HPP  

2、
[cpp] view plain copy
  1. //  
  2. // request_parser.cpp  
  3. // ~~~~~~~~~~~~~~~~~~  
  4. //  
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)  
  6. //  
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying  
  8. // file LICENSE_1_0.txt or copy at   
  9. //  
  10.   
  11. #include "request_parser.hpp"  
  12. #include "request.hpp"  
  13.   
  14. namespace http {  
  15. namespace server3 {  
  16.   
  17. request_parser::request_parser()  
  18.   : state_(method_start)  
  19. {  
  20. }  
  21.   
  22. void request_parser::reset()  
  23. {  
  24.   state_ = method_start;  
  25. }  
  26.   
  27. boost::tribool request_parser::consume(request& req, char input)  
  28. {  
  29.   switch (state_)  
  30.   {  
  31.   case method_start:  
  32.     if (!is_char(input) || is_ctl(input) || is_tspecial(input))  
  33.     {  
  34.       return false;  
  35.     }  
  36.     else  
  37.     {  
  38.       state_ = method;  
  39.       req.method.push_back(input);  
  40.       return boost::indeterminate;  
  41.     }  
  42.   case method:  
  43.     if (input == ' ')  
  44.     {  
  45.       state_ = uri;  
  46.       return boost::indeterminate;  
  47.     }  
  48.     else if (!is_char(input) || is_ctl(input) || is_tspecial(input))  
  49.     {  
  50.       return false;  
  51.     }  
  52.     else  
  53.     {  
  54.       req.method.push_back(input);  
  55.       return boost::indeterminate;  
  56.     }  
  57.   case uri_start:  
  58.     if (is_ctl(input))  
  59.     {  
  60.       return false;  
  61.     }  
  62.     else  
  63.     {  
  64.       state_ = uri;  
  65.       req.uri.push_back(input);  
  66.       return boost::indeterminate;  
  67.     }  
  68.   case uri:  
  69.     if (input == ' ')  
  70.     {  
  71.       state_ = http_version_h;  
  72.       return boost::indeterminate;  
  73.     }  
  74.     else if (is_ctl(input))  
  75.     {  
  76.       return false;  
  77.     }  
  78.     else  
  79.     {  
  80.       req.uri.push_back(input);  
  81.       return boost::indeterminate;  
  82.     }  
  83.   case http_version_h:  
  84.     if (input == 'H')  
  85.     {  
  86.       state_ = http_version_t_1;  
  87.       return boost::indeterminate;  
  88.     }  
  89.     else  
  90.     {  
  91.       return false;  
  92.     }  
  93.   case http_version_t_1:  
  94.     if (input == 'T')  
  95.     {  
  96.       state_ = http_version_t_2;  
  97.       return boost::indeterminate;  
  98.     }  
  99.     else  
  100.     {  
  101.       return false;  
  102.     }  
  103.   case http_version_t_2:  
  104.     if (input == 'T')  
  105.     {  
  106.       state_ = http_version_p;  
  107.       return boost::indeterminate;  
  108.     }  
  109.     else  
  110.     {  
  111.       return false;  
  112.     }  
  113.   case http_version_p:  
  114.     if (input == 'P')  
  115.     {  
  116.       state_ = http_version_slash;  
  117.       return boost::indeterminate;  
  118.     }  
  119.     else  
  120.     {  
  121.       return false;  
  122.     }  
  123.   case http_version_slash:  
  124.     if (input == '/')  
  125.     {  
  126.       req.http_version_major = 0;  
  127.       req.http_version_minor = 0;  
  128.       state_ = http_version_major_start;  
  129.       return boost::indeterminate;  
  130.     }  
  131.     else  
  132.     {  
  133.       return false;  
  134.     }  
  135.   case http_version_major_start:  
  136.     if (is_digit(input))  
  137.     {  
  138.       req.http_version_major = req.http_version_major * 10 + input - '0';  
  139.       state_ = http_version_major;  
  140.       return boost::indeterminate;  
  141.     }  
  142.     else  
  143.     {  
  144.       return false;  
  145.     }  
  146.   case http_version_major:  
  147.     if (input == '.')  
  148.     {  
  149.       state_ = http_version_minor_start;  
  150.       return boost::indeterminate;  
  151.     }  
  152.     else if (is_digit(input))  
  153.     {  
  154.       req.http_version_major = req.http_version_major * 10 + input - '0';  
  155.       return boost::indeterminate;  
  156.     }  
  157.     else  
  158.     {  
  159.       return false;  
  160.     }  
  161.   case http_version_minor_start:  
  162.     if (is_digit(input))  
  163.     {  
  164.       req.http_version_minor = req.http_version_minor * 10 + input - '0';  
  165.       state_ = http_version_minor;  
  166.       return boost::indeterminate;  
  167.     }  
  168.     else  
  169.     {  
  170.       return false;  
  171.     }  
  172.   case http_version_minor:  
  173.     if (input == '\r')  
  174.     {  
  175.       state_ = expecting_newline_1;  
  176.       return boost::indeterminate;  
  177.     }  
  178.     else if (is_digit(input))  
  179.     {  
  180.       req.http_version_minor = req.http_version_minor * 10 + input - '0';  
  181.       return boost::indeterminate;  
  182.     }  
  183.     else  
  184.     {  
  185.       return false;  
  186.     }  
  187.   case expecting_newline_1:  
  188.     if (input == '\n')  
  189.     {  
  190.       state_ = header_line_start;  
  191.       return boost::indeterminate;  
  192.     }  
  193.     else  
  194.     {  
  195.       return false;  
  196.     }  
  197.   case header_line_start:  
  198.     if (input == '\r')  
  199.     {  
  200.       state_ = expecting_newline_3;  
  201.       return boost::indeterminate;  
  202.     }  
  203.     else if (!req.headers.empty() && (input == ' ' || input == '\t'))  
  204.     {  
  205.       state_ = header_lws;  
  206.       return boost::indeterminate;  
  207.     }  
  208.     else if (!is_char(input) || is_ctl(input) || is_tspecial(input))  
  209.     {  
  210.       return false;  
  211.     }  
  212.     else  
  213.     {  
  214.       req.headers.push_back(header());  
  215.       req.headers.back().name.push_back(input);  
  216.       state_ = header_name;  
  217.       return boost::indeterminate;  
  218.     }  
  219.   case header_lws:  
  220.     if (input == '\r')  
  221.     {  
  222.       state_ = expecting_newline_2;  
  223.       return boost::indeterminate;  
  224.     }  
  225.     else if (input == ' ' || input == '\t')  
  226.     {  
  227.       return boost::indeterminate;  
  228.     }  
  229.     else if (is_ctl(input))  
  230.     {  
  231.       return false;  
  232.     }  
  233.     else  
  234.     {  
  235.       state_ = header_value;  
  236.       req.headers.back().value.push_back(input);  
  237.       return boost::indeterminate;  
  238.     }  
  239.   case header_name:  
  240.     if (input == ':')  
  241.     {  
  242.       state_ = space_before_header_value;  
  243.       return boost::indeterminate;  
  244.     }  
  245.     else if (!is_char(input) || is_ctl(input) || is_tspecial(input))  
  246.     {  
  247.       return false;  
  248.     }  
  249.     else  
  250.     {  
  251.       req.headers.back().name.push_back(input);  
  252.       return boost::indeterminate;  
  253.     }  
  254.   case space_before_header_value:  
  255.     if (input == ' ')  
  256.     {  
  257.       state_ = header_value;  
  258.       return boost::indeterminate;  
  259.     }  
  260.     else  
  261.     {  
  262.       return false;  
  263.     }  
  264.   case header_value:  
  265.     if (input == '\r')  
  266.     {  
  267.       state_ = expecting_newline_2;  
  268.       return boost::indeterminate;  
  269.     }  
  270.     else if (is_ctl(input))  
  271.     {  
  272.       return false;  
  273.     }  
  274.     else  
  275.     {  
  276.       req.headers.back().value.push_back(input);  
  277.       return boost::indeterminate;  
  278.     }  
  279.   case expecting_newline_2:  
  280.     if (input == '\n')  
  281.     {  
  282.       state_ = header_line_start;  
  283.       return boost::indeterminate;  
  284.     }  
  285.     else  
  286.     {  
  287.       return false;  
  288.     }  
  289.   case expecting_newline_3:  
  290.     return (input == '\n');  
  291.   case expecting_newline_4: //针对post请求做处理  
  292.       {  
  293.         req.post.push_back(input);  
  294.         if(input=='}') {  
  295.             state_ = none; //对post请求处理完成。  
  296.             return true;  
  297.         }  
  298.         return boost::indeterminate;  
  299.       }  
  300.   default:  
  301.     return false;  
  302.   }  
  303. }  
  304.   
  305. bool request_parser::is_char(int c)  
  306. {  
  307.   return c >= 0 && c <= 127;  
  308. }  
  309.   
  310. bool request_parser::is_ctl(int c)  
  311. {  
  312.   return c >= 0 && c <= 31 || c == 127;  
  313. }  
[cpp] view plain copy
  1. bool request_parser::is_tspecial(int c)  
[cpp] view plain copy
  1. {  
  2.   switch (c)  
  3.   {  
  4.   case '('case ')'case '<'case '>'case '@':  
  5.   case ','case ';'case ':'case '\\': case '"':  
  6.   case '/'case '['case ']'case '?'case '=':  
  7.   case '{'case '}'case ' 'case '\t':  
  8.     return true;  
  9.   default:  
  10.     return false;  
  11.   }  
  12. }  
  13.   
  14. bool request_parser::is_digit(int c)  
  15. {  
  16.   return c >= '0' && c <= '9';  
  17. }  
  18.   
  19. // namespace server3  
  20. // namespace http  
3、



3、针对request_handler类增加了自己处理客户端请求函数,不用官方的

[cpp] view plain copy
  1. void request_handler::handle_request(const request& req)  
  2. {  
  3.     if(req.method=="POST"){  
  4.      //json解析  
  5.         Json::Reader reader;  
  6.     Json::Value jsonval;  
  7.     if(!(reader.parse(req.post,jsonval)))  
  8.     {  
  9.         std::cerr << "json解析错误:" << req.post << std::endl;  
  10.         return ;  
  11.     }  
  12.     std::string funcid = jsonval["x"].asString();  
  13.     std::string content = jsonval["y"].asString();  
  14.     std::cout << "收到客户端请求:" <<req.post<< std::endl;  
  15.     }else{  
  16.     std::cout << "收到客户端请求:" <<req.uri.substr(2,req.uri.length())<< std::endl;  
  17.     std::string str = req.uri;  
  18.     std::vector<std::string> vecSegTag;  
  19.     boost::split(vecSegTag, str, boost::is_any_of(("/?&=")));  
  20.     m_MapContent.clear();  
  21.     for(int i = 2; i < vecSegTag.size() - 1; ){  
  22.        m_MapContent.insert(std::pair<std::string, std::string>(vecSegTag[i], vecSegTag[i+1]));  
  23.        i += 2;  
  24.     }  
  25.     }  
  26. }  


4、connection类修改handle_read函数增加自己处理客户的请求函数

[cpp] view plain copy
  1. void connection::handle_read(const boost::system::error_code& e,  
  2.     std::size_t bytes_transferred)  
  3. {  
  4.   if (!e)  
  5.   {     
  6.     boost::tribool result;  
  7.     boost::tie(result, boost::tuples::ignore) = request_parser_.parse(  
  8.         request_, buffer_.data(), buffer_.data() + bytes_transferred);  
  9.       
  10.     buffer_.assign('\0');  
  11.     if (result)  
  12.     {  
  13.         request_handler_.handle_request(request_);  
  14.         //std::map<std::string,std::string> tmp = request_handler_.getRequestContent();  
  15.     /*  request_handler_.handle_request(request_, reply_);*/  
  16.          // Fill out the reply to be sent to the client.  
  17.   reply_.status = reply::ok;  
  18.   char buf[512] = {"hello"}; //回应客户端数据  
  19.    
  20.     reply_.content.append(buf);  
  21.   reply_.headers.resize(2);  
  22.   reply_.headers[0].name = "Content-Length";  
  23.   reply_.headers[0].value = boost::lexical_cast<std::string>(reply_.content.size());  
  24.   reply_.headers[1].name = "Content-Type";  
  25.   reply_.headers[1].value = mime_types::extension_to_type("");  
  26.   
  27.       boost::asio::async_write(socket_, reply_.to_buffers(),  
  28.           strand_.wrap(  
  29.             boost::bind(&connection::handle_write, shared_from_this(),  
  30.               boost::asio::placeholders::error)));  
  31.     }  
  32.     else if (!result)  
  33.     {  
  34.       reply_ = reply::stock_reply(reply::bad_request);  
  35.       boost::asio::async_write(socket_, reply_.to_buffers(),  
  36.           strand_.wrap(  
  37.             boost::bind(&connection::handle_write, shared_from_this(),  
  38.               boost::asio::placeholders::error)));  
  39.     }  
  40.     else  
  41.     {  
  42.       socket_.async_read_some(boost::asio::buffer(buffer_),  
  43.           strand_.wrap(  
  44.             boost::bind(&connection::handle_read, shared_from_this(),  
  45.               boost::asio::placeholders::error,  
  46.               boost::asio::placeholders::bytes_transferred)));  
  47.     }  
  48.   }  
  49.   
  50.   // If an error occurs then no new asynchronous operations are started. This  
  51.   // means that all shared_ptr references to the connection object will  
  52.   // disappear and the object will be destroyed automatically after this  
  53.   // handler returns. The connection class's destructor closes the socket.  
  54. }  


3、针对request_handler类增加了自己处理客户端请求函数,不用官方的

[cpp] view plain copy
  1. void request_handler::handle_request(const request& req)  
  2. {  
  3.     if(req.method=="POST"){  
  4.      //json解析  
  5.         Json::Reader reader;  
  6.     Json::Value jsonval;  
  7.     if(!(reader.parse(req.post,jsonval)))  
  8.     {  
  9.         std::cerr << "json解析错误:" << req.post << std::endl;  
  10.         return ;  
  11.     }  
  12.     std::string funcid = jsonval["x"].asString();  
  13.     std::string content = jsonval["y"].asString();  
  14.     std::cout << "收到客户端请求:" <<req.post<< std::endl;  
  15.     }else{  
  16.     std::cout << "收到客户端请求:" <<req.uri.substr(2,req.uri.length())<< std::endl;  
  17.     std::string str = req.uri;  
  18.     std::vector<std::string> vecSegTag;  
  19.     boost::split(vecSegTag, str, boost::is_any_of(("/?&=")));  
  20.     m_MapContent.clear();  
  21.     for(int i = 2; i < vecSegTag.size() - 1; ){  
  22.        m_MapContent.insert(std::pair<std::string, std::string>(vecSegTag[i], vecSegTag[i+1]));  
  23.        i += 2;  
  24.     }  
  25.     }  
  26. }  

最新评论

ye609337363 发表于 2022-5-6 13:52
HTML源码下载

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表