in httpdns_api_demo/src/main/java/alibaba/httpdns_api_demo/HttpDNS.java [164:214]
public String call() {
String resolveUrl = "http://" + SERVER_IP + "/" + ACCOUNT_ID + "/d?host=" + hostName;
HttpDNSLog.logD("[QueryHostTask.call] - buildUrl: " + resolveUrl);
try {
HttpURLConnection conn = (HttpURLConnection) new URL(resolveUrl).openConnection();
conn.setConnectTimeout(RESOLVE_TIMEOUT_IN_SEC * 1000);
conn.setReadTimeout(RESOLVE_TIMEOUT_IN_SEC * 1000);
if (conn.getResponseCode() != 200) {
HttpDNSLog.logW("[QueryHostTask.call] - response code: " + conn.getResponseCode());
} else {
InputStream in = conn.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = streamReader.readLine()) != null) {
sb.append(line);
}
JSONObject json = new JSONObject(sb.toString());
String host = json.getString("host");
long ttl = json.getLong("ttl");
JSONArray ips = json.getJSONArray("ips");
if (host != null) {
if (ttl == 0) {
// 如果有结果返回,但是ip列表为空,ttl也为空,那默认没有ip就是解析结果,并设置ttl为一个较长的时间
// 避免一直请求同一个ip冲击sever
ttl = EMPTY_RESULT_HOST_TTL;
}
HostObject hostObject = new HostObject();
String ip = (ips == null) ? null : ips.getString(0);
HttpDNSLog.logD("[QueryHostTask.call] - resolve host:" + host + " ip:" + ip + " ttl:" + ttl);
hostObject.setHostName(host);
hostObject.setTtl(ttl);
hostObject.setIp(ip);
hostObject.setQueryTime(System.currentTimeMillis() / 1000);
if (hostManager.size() < MAX_HOLD_HOST_NUM) {
hostManager.put(hostName, hostObject);
}
return ip;
}
}
} catch (Exception e) {
if (HttpDNSLog.isLogEnabled()) {
e.printStackTrace();
}
}
if (!isRequestRetried) {
isRequestRetried = true;
return call();
}
return null;
}