| 77 | |
| 78 | |
| 79 | int SendHttpQuery(const std::string& _url, int& _status_code, std::string& _errmsg, int _timeout) { |
| 80 | xinfo2(TSF"httpQuery:_url=%_", _url); |
| 81 | if (!strutil::StartsWith(_url, "http://")) { |
| 82 | xerror2(TSF"url is not start with http://"); |
| 83 | _errmsg.append("url is not start with http://"); |
| 84 | return -2; |
| 85 | } |
| 86 | unsigned long long timebegin = gettickcount(); |
| 87 | |
| 88 | if (_timeout <= 0) { |
| 89 | _timeout = HTTP_DEFAULT_TIMEOUT; |
| 90 | } |
| 91 | HttpUrlParser http_url_parser(_url); |
| 92 | std::string host = http_url_parser.Host(); |
| 93 | xdebug2(TSF"host=%0", host); |
| 94 | |
| 95 | std::string str_req(""); |
| 96 | http::RequestLine reqLine(http::RequestLine::kGet, http_url_parser.Path(), http::kVersion_1_1); |
| 97 | str_req.append(reqLine.ToString()); |
| 98 | |
| 99 | http::HeaderFields header; |
| 100 | header.HeaderFiled("Accept", "text/html, application/xhtml+xml, */*"); |
| 101 | header.HeaderFiled("Accept-Language", "zh-CN"); |
| 102 | header.HeaderFiled("User-Agent", USER_AGENT); |
| 103 | header.HeaderFiled("Accept-Encoding", "gzip, deflate"); |
| 104 | header.HeaderFiled("Proxy-Connection", "Keep-Alive"); |
| 105 | |
| 106 | bool domain_is_ipaddr = socket_address(host.c_str(), 0).valid(); // 判断strHost是否是一个点分十进制IP |
| 107 | |
| 108 | header.HeaderFiled("Host", host.c_str()); |
| 109 | str_req.append(header.ToString()); |
| 110 | str_req.append("\r\n\r\n"); // important |
| 111 | |
| 112 | xdebug2(TSF"str_req=%_", str_req); |
| 113 | |
| 114 | unsigned int port = http_url_parser.Port(); |
| 115 | |
| 116 | struct socket_ipinfo_t ipinfo; |
| 117 | |
| 118 | char ip[20] = {0}; |
| 119 | |
| 120 | int ret = 0; |
| 121 | |
| 122 | do { |
| 123 | if (!domain_is_ipaddr) { |
| 124 | unsigned long long timespan1 = gettickspan(timebegin); |
| 125 | |
| 126 | if ((unsigned int)_timeout <= timespan1) { |
| 127 | xwarn2(TSF"check http timeout."); |
| 128 | _errmsg.append("check http timeout."); |
| 129 | ret = -1; |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | if (0 == socket_gethostbyname(host.c_str(), &ipinfo, (int)(_timeout - timespan1), NULL)) { |
| 134 | strncpy(ip, socket_address(ipinfo.ip[0]).ip(), sizeof(ip)); |
| 135 | } else { |
| 136 | xerror2(TSF"check http get DNS error."); |
no test coverage detected