贴上微信小程序发送http请求代码:
onsend: function(){
wx.request({
url: 'http://127.0.0.1:1000', //c++后台ip、端口
data: {
x: '1' , //发送到后台字段
y: '2'
},
header:{
"Content-Type":"application/json"
},
method:"POST", //发送POST,若为GET则改为GET
success: function(res) {
var data = res.data;
console.log(data);
}
});
}
c++后台代码借鉴boost官网的asio的http server 3地址并自己做了修改:
官网地址:http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/examples.html
修改代码:
1、request_parser.hpp:
//
// request_parser.hpp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef HTTP_SERVER3_REQUEST_PARSER_HPP
#define HTTP_SERVER3_REQUEST_PARSER_HPP
#include <boost/logic/tribool.hpp>
#include <boost/tuple/tuple.hpp>
namespace http {
namespace server3 {
struct request;
/// Parser for incoming requests.
class request_parser
{
public:
/// Construct ready to parse the request method.
request_parser();
/// Reset to initial parser state.
void reset();
/// Parse some data. The tribool return value is true when a complete request
/// has been parsed, false if the data is invalid, indeterminate when more
/// data is required. The InputIterator return value indicates how much of the
/// input has been consumed.
template <typename InputIterator>
boost::tuple<boost::tribool, InputIterator> parse(request& req,
InputIterator begin, InputIterator end)
{
if(req.method=="POST") state_ = expecting_newline_4; //自己增加针对post请求数据一次性接收不完问题
while (begin != end)
{
boost::tribool result = consume(req, *begin++);
if (state_ != none && (result || !result)){ //针对post请求做特殊处理
if(req.method=="POST"){
char c = *begin;
if(c=='\0') break;
state_ = expecting_newline_4;
result = consume(req, *begin++);
}
}
if (result || !result){
return boost::make_tuple(result, begin);
}
}
boost::tribool result = boost::indeterminate;
return boost::make_tuple(result, begin);
}
private:
/// Handle the next character of input.
boost::tribool consume(request& req, char input);
/// Check if a byte is an HTTP character.
static bool is_char(int c);
/// Check if a byte is an HTTP control character.
static bool is_ctl(int c);
/// Check if a byte is defined as an HTTP tspecial character.
static bool is_tspecial(int c);
/// Check if a byte is a digit.
static bool is_digit(int c);
/// The current state of the parser.
enum state
{
method_start,
method,
uri_start,
uri,
http_version_h,
http_version_t_1,
http_versio