正在显示
4 个修改的文件
包含
64 行增加
和
22 行删除
| ... | ... | @@ -54,25 +54,25 @@ if (temp_data.find(http_) == 0) { |
| 54 | 54 | int DmpHttp::DmpHttpBase::buildPostRequest(const std::string& server, const std::string& path, |
| 55 | 55 | std::ostream& out_request) { |
| 56 | 56 | // 分割path中的json数据 |
| 57 | - // std::string temp_path(path), temp_json; | |
| 58 | - // int json_pos_begin = temp_path.find(HTTP_JSON_BEGIN) + 1; | |
| 59 | - // int json_pos_end = temp_path.find(HTTP_JSON_END); | |
| 60 | - // if (json_pos_begin != std::string::npos) { | |
| 61 | - // // 计算json的长度 | |
| 62 | - // int temp_json_lenth = std::string::npos; | |
| 63 | - // if (json_pos_end != temp_json_lenth) { | |
| 64 | - // temp_json_lenth = (json_pos_end - json_pos_begin); | |
| 65 | - // } | |
| 66 | - // temp_json = temp_path.substr(json_pos_begin, temp_json_lenth); | |
| 67 | - // temp_path = temp_path.substr(0, (json_pos_begin - 1)); | |
| 68 | - // } | |
| 69 | - // out_request << "POST " << temp_path.c_str() << " HTTP/1.0\r\n"; | |
| 70 | - // out_request << "Host: " << server.c_str() << "\r\n"; | |
| 71 | - // out_request << "Content-Length: " << temp_json.length() << "\r\n"; | |
| 72 | - // out_request << "Content-Type: application/x-www-form-urlencoded\r\n"; | |
| 73 | - // out_request << "Accept: */*\r\n"; | |
| 74 | - // out_request << "Connection: close\r\n\r\n"; | |
| 75 | - // out_request << temp_json.c_str(); | |
| 57 | + std::string temp_path(path), temp_json; | |
| 58 | + int json_pos_begin = temp_path.find(HTTP_JSON_BEGIN) + 1; | |
| 59 | + int json_pos_end = temp_path.find(HTTP_JSON_END); | |
| 60 | + if (json_pos_begin != std::string::npos) { | |
| 61 | + // 计算json的长度 | |
| 62 | + int temp_json_lenth = std::string::npos; | |
| 63 | + if (json_pos_end != temp_json_lenth) { | |
| 64 | + temp_json_lenth = (json_pos_end - json_pos_begin); | |
| 65 | + } | |
| 66 | + temp_json = temp_path.substr(json_pos_begin, temp_json_lenth); | |
| 67 | + temp_path = temp_path.substr(0, (json_pos_begin - 1)); | |
| 68 | + } | |
| 69 | + out_request << "POST " << temp_path.c_str() << " HTTP/1.0\r\n"; | |
| 70 | + out_request << "Host: " << server.c_str() << "\r\n"; | |
| 71 | + out_request << "Content-Length: " << temp_json.length() << "\r\n"; | |
| 72 | + out_request << "Content-Type: application/x-www-form-urlencoded\r\n"; | |
| 73 | + out_request << "Accept: */*\r\n"; | |
| 74 | + out_request << "Connection: close\r\n\r\n"; | |
| 75 | + out_request << temp_json.c_str(); | |
| 76 | 76 | return HTTP_SUCCESS; |
| 77 | 77 | } |
| 78 | 78 | ... | ... |
| ... | ... | @@ -11,8 +11,8 @@ |
| 11 | 11 | #include <iostream> |
| 12 | 12 | #include <boost/asio.hpp> |
| 13 | 13 | namespace DmpHttp |
| 14 | -{ | |
| 15 | - #define HTTP_SUCCESS (0) // 操作成功 | |
| 14 | +{ | |
| 15 | + #define HTTP_SUCCESS (0) // 操作成功 | |
| 16 | 16 | #define HTTP_ERROR_UNKNOWN (-1) // 未知的错误 |
| 17 | 17 | #define HTTP_ERROR_NETWORK (-2) // 网络连接失败 |
| 18 | 18 | #define HTTP_ERROR_HTTP_HEAD (-3) // 未找到协议头 http || https |
| ... | ... | @@ -35,6 +35,7 @@ namespace DmpHttp |
| 35 | 35 | virtual ~DmpHttpBase(); |
| 36 | 36 | // Post请求 |
| 37 | 37 | virtual int post(const std::string& url) = 0; |
| 38 | + virtual int post(const std::string& url, std::string& postData)= 0; | |
| 38 | 39 | // get请求 |
| 39 | 40 | virtual int get(const std::string& url) = 0; |
| 40 | 41 | virtual std::string getResponse(void) = 0; | ... | ... |
| ... | ... | @@ -18,6 +18,11 @@ int DmpHttp::DmpHttpUtils::post(const std::string& url) |
| 18 | 18 | handleRequestResolve(url, DmpHttpBase::buildPostRequest); |
| 19 | 19 | return HTTP_SUCCESS; |
| 20 | 20 | } |
| 21 | +int DmpHttp::DmpHttpUtils::post(const std::string& url, std::string& postData) | |
| 22 | +{ | |
| 23 | + handleRequestResolve(url,postData, DmpHttpBase::buildPostRequest); | |
| 24 | + return HTTP_SUCCESS; | |
| 25 | +} | |
| 21 | 26 | int DmpHttp::DmpHttpUtils::get(const std::string& url) |
| 22 | 27 | { |
| 23 | 28 | handleRequestResolve(url, DmpHttpBase::buildGetRequest); |
| ... | ... | @@ -47,6 +52,32 @@ void DmpHttp::DmpHttpUtils::handleRequestResolve(const std::string& url, pBuildR |
| 47 | 52 | } |
| 48 | 53 | return; |
| 49 | 54 | } |
| 55 | +void DmpHttp::DmpHttpUtils::handleRequestResolve(const std::string& url, std::string& postData, pBuildRequest func) | |
| 56 | +{ | |
| 57 | + try { | |
| 58 | + responseData_.clear(); | |
| 59 | + // 解析URL | |
| 60 | + std::string server, port,path; | |
| 61 | + parseUrl(url, server, port, path); | |
| 62 | + path=postData; | |
| 63 | + | |
| 64 | + std::ostream request_stream(&request_); | |
| 65 | + // 合成请求 | |
| 66 | + func(server, path, request_stream); | |
| 67 | + | |
| 68 | + // 解析服务地址\端口 | |
| 69 | + boost::asio::ip::tcp::resolver::query query(server, port); | |
| 70 | + resolver_.async_resolve(query, | |
| 71 | + boost::bind(&DmpHttpUtils::handleResolve, this, | |
| 72 | + boost::asio::placeholders::error, | |
| 73 | + boost::asio::placeholders::iterator)); | |
| 74 | + } | |
| 75 | + catch (std::exception& e) { | |
| 76 | + socket_.close(); | |
| 77 | + std::cout << "Exception: " << e.what() << "\n"; | |
| 78 | + } | |
| 79 | + return; | |
| 80 | +} | |
| 50 | 81 | void DmpHttp::DmpHttpUtils::handleResolve(const boost::system::error_code& err, |
| 51 | 82 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator) { |
| 52 | 83 | if (err) { |
| ... | ... | @@ -173,7 +204,14 @@ std::string DmpHttp::post(const std::string &url) { |
| 173 | 204 | io.run(); |
| 174 | 205 | return c.getResponse(); |
| 175 | 206 | } |
| 176 | - | |
| 207 | +std::string DmpHttp::post(const std::string& url, std::string& postData) | |
| 208 | +{ | |
| 209 | + boost::asio::io_service io; | |
| 210 | + DmpHttp::DmpHttpUtils c(io); | |
| 211 | + c.post(url,postData); | |
| 212 | + io.run(); | |
| 213 | + return c.getResponse(); | |
| 214 | +} | |
| 177 | 215 | std::string DmpHttp::get(const std::string &url) { |
| 178 | 216 | boost::asio::io_service io; |
| 179 | 217 | DmpHttp::DmpHttpUtils c(io); | ... | ... |
| ... | ... | @@ -20,12 +20,14 @@ namespace DmpHttp |
| 20 | 20 | DmpHttpUtils(boost::asio::io_service& io_service); |
| 21 | 21 | virtual ~DmpHttpUtils(); |
| 22 | 22 | virtual int post(const std::string& url); |
| 23 | + virtual int post(const std::string& url, std::string& postData); | |
| 23 | 24 | virtual int get(const std::string& url); |
| 24 | 25 | |
| 25 | 26 | virtual std::string getResponse(void) {return responseData_;} |
| 26 | 27 | private: |
| 27 | 28 | // 建立请求 |
| 28 | 29 | void handleRequestResolve(const std::string& url, pBuildRequest func); |
| 30 | + void handleRequestResolve(const std::string& url, std::string& path, pBuildRequest func); | |
| 29 | 31 | // 解析后 |
| 30 | 32 | void handleResolve(const boost::system::error_code& err, |
| 31 | 33 | boost::asio::ip::tcp::resolver::iterator endpoint_iterator); |
| ... | ... | @@ -52,6 +54,7 @@ namespace DmpHttp |
| 52 | 54 | std::string responseData_; |
| 53 | 55 | }; |
| 54 | 56 | std::string post(const std::string &url); |
| 57 | + std::string post(const std::string& url, std::string& postData); | |
| 55 | 58 | std::string get(const std::string &url); |
| 56 | 59 | } |
| 57 | 60 | ... | ... |
请
注册
或
登录
后发表评论