in httpdns_api_demo/src/main/java/alibaba/httpdns_api_demo/NetworkRequestUsingHttpDNS.java [18:60]
public static void main(final Context ctx) {
try {
HttpDNS.HttpDNSLog.enableLog(true);
// DegradationFilter用于自定义降级逻辑
// 通过实现shouldDegradeHttpDNS方法,可以根据需要,选择是否降级
DegradationFilter filter = new DegradationFilter() {
@Override
public boolean shouldDegradeHttpDNS(String hostName) {
// 此处可以自定义降级逻辑,例如www.taobao.com不使用HttpDNS解析
// 参照HttpDNS API文档,当存在中间HTTP代理时,应选择降级,使用Local DNS
return hostName.equals("www.taobao.com") || detectIfProxyExist(ctx);
}
};
// 将filter传进httpdnsService,解析时会回调shouldDegradeHttpDNS方法,判断是否降级
httpdnsService.setDegradationFilter(filter);
byte[] buff = new byte[4096];
HttpURLConnection conn;
int responseCode;
for (int i = 0; i < 2; i++) {
conn = getHttpURLConnection(TEST_URL[i]);
responseCode = conn.getResponseCode();
Log.d("HttpDNS Demo", "Response code: " + responseCode);
if (responseCode == 200) {
InputStream in = conn.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = dis.read(buff)) != -1) {
sb.append(new String(buff, 0, len));
}
Log.d("HttpDNS Demo", "Get result: " + sb);
}
conn.disconnect();
Thread.sleep(3000);
}
} catch (Exception e) {
e.printStackTrace();
}
}