| 31 | } |
| 32 | |
| 33 | bool HttpClient::send() |
| 34 | { |
| 35 | // 创建 HTTP 请求客户端 |
| 36 | acl::http_request req(server_addr_); |
| 37 | acl::http_header& hdr = req.request_header(); |
| 38 | |
| 39 | // 设置请求的 URL |
| 40 | hdr.set_url("/"); |
| 41 | |
| 42 | // 设置 HTTP 请求头的字段 |
| 43 | hdr.set_content_type("text/json; charset=gb2312"); |
| 44 | hdr.set_keep_alive(false); |
| 45 | |
| 46 | // 发送数据体 |
| 47 | if (req.request(buf_->c_str(), buf_->length()) == false) |
| 48 | { |
| 49 | logger_error("request to server error, addr: %s", |
| 50 | server_addr_.c_str()); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | int status = req.http_status(); |
| 55 | if (status != 200) |
| 56 | { |
| 57 | logger_error("server(%s) return status: %d", |
| 58 | server_addr_.c_str(), status); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | long long int length = req.body_length(); |
| 63 | if (length <= 0) |
| 64 | return true; |
| 65 | |
| 66 | // 将响应数据体读完 |
| 67 | acl::string buf; |
| 68 | if (req.get_body(buf) == false) |
| 69 | { |
| 70 | logger_error("read response body failed!"); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
no test coverage detected