带阿里标识的版本

This commit is contained in:
robin
2026-02-28 18:55:33 +08:00
parent 150799f41d
commit 5d0b7c7e91
477 changed files with 10813 additions and 4044 deletions

View File

@@ -26,7 +26,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
forTest {
// 注意这里的配置并不是需要编译forTest的app而是避免httpdns-sdk在AndroidStudio改为end2end运行测试时 BuildVariants报错
// 娉ㄦ剰杩欓噷鐨勯厤缃紝骞朵笉鏄渶瑕佺紪璇慺orTest鐨刟pp锛岃€屾槸閬垮厤httpdns-sdk鍦ˋndroidStudio鏀逛负end2end杩愯娴嬭瘯鏃?BuildVariants鎶ラ敊
initWith release
debuggable true
}
@@ -67,7 +67,7 @@ android {
}
end2end {
// 注意这里的配置并不是需要编译end2end的app而是避免httpdns-sdk在AndroidStudio改为end2end运行测试时 BuildVariants报错
// 娉ㄦ剰杩欓噷鐨勯厤缃紝骞朵笉鏄渶瑕佺紪璇慹nd2end鐨刟pp锛岃€屾槸閬垮厤httpdns-sdk鍦ˋndroidStudio鏀逛负end2end杩愯娴嬭瘯鏃?BuildVariants鎶ラ敊
}
}
}

View File

@@ -33,10 +33,10 @@ public class HttpDnsActivity extends BaseActivity {
public static final String SCHEMA_HTTP = "http://";
public static final String TAOBAO_URL = "www.taobao.com";
public static final String ALIYUN_URL = "www.aliyun.com";
public static final String Aliyun_URL = "www.Aliyun.com";
public static final String[] hosts = new String[]{
TAOBAO_URL, ALIYUN_URL
TAOBAO_URL, Aliyun_URL
};
public static String getUrl(String schema, String host) {
@@ -44,15 +44,15 @@ public class HttpDnsActivity extends BaseActivity {
}
/**
* 要请求的schema
* 瑕佽姹傜殑schema
*/
private String schema = SCHEMA_HTTPS;
/**
* 要请求的域名
* 瑕佽姹傜殑鍩熷悕
*/
private String host = ALIYUN_URL;
private String host = Aliyun_URL;
/**
* 要解析的ip类型
* 瑕佽В鏋愮殑ip绫诲瀷
*/
private RequestIpType requestIpType = RequestIpType.v4;
@@ -69,77 +69,77 @@ public class HttpDnsActivity extends BaseActivity {
okHttpRequest = new OkHttpRequest(this);
networkRequest = httpUrlConnectionRequest;
addFourButton("切换实例", v -> {
addFourButton("鍒囨崲瀹炰緥", v -> {
MyApp.getInstance().changeHolder();
sendLog("httpdns实例已切换");
}, "获取配置", v -> {
sendLog("httpdns瀹炰緥宸插垏鎹?);
}, "鑾峰彇閰嶇疆", v -> {
sendLog(MyApp.getInstance().getCurrentHolder().getCurrentConfig());
sendLog("要解析的域名是" + host);
sendLog("要解析的ip类型是" + requestIpType.name());
sendLog("要模拟请求的url是" + getUrl(schema, host));
sendLog("模拟请求的网络框架是" + (networkRequest == httpUrlConnectionRequest ? " HttpUrlConnection" : "OkHttp"));
}, "清除配置缓存", v -> {
sendLog("瑕佽В鏋愮殑鍩熷悕鏄? + host);
sendLog("瑕佽В鏋愮殑ip绫诲瀷鏄? + requestIpType.name());
sendLog("瑕佹ā鎷熻姹傜殑url鏄? + getUrl(schema, host));
sendLog("妯℃嫙璇锋眰鐨勭綉缁滄鏋舵槸" + (networkRequest == httpUrlConnectionRequest ? " HttpUrlConnection" : "OkHttp"));
}, "娓呴櫎閰嶇疆缂撳瓨", v -> {
MyApp.getInstance().getCurrentHolder().cleanSp();
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "配置缓存清除, 重启生效");
}, "清除日志", v -> cleanLog());
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "閰嶇疆缂撳瓨娓呴櫎, 閲嶅惎鐢熸晥");
}, "娓呴櫎鏃ュ織", v -> cleanLog());
addTwoButton("开启https", v -> {
addTwoButton("寮€鍚痟ttps", v -> {
MyApp.getInstance().getCurrentHolder().setEnableHttps(true);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "开启https");
}, "关闭https", v -> {
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "寮€鍚痟ttps");
}, "鍏抽棴https", v -> {
MyApp.getInstance().getCurrentHolder().setEnableHttps(false);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "关闭https");
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "鍏抽棴https");
});
addTwoButton("允许过期IP", v -> {
addTwoButton("鍏佽杩囨湡IP", v -> {
MyApp.getInstance().getCurrentHolder().setEnableExpiredIp(true);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "允许过期IP");
}, "不允许过期IP", v -> {
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "鍏佽杩囨湡IP");
}, "涓嶅厑璁歌繃鏈烮P", v -> {
MyApp.getInstance().getCurrentHolder().setEnableExpiredIp(false);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "不允许过期IP");
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "涓嶅厑璁歌繃鏈烮P");
});
addTwoButton("允许持久化缓存", v -> {
addTwoButton("鍏佽鎸佷箙鍖栫紦瀛?, v -> {
MyApp.getInstance().getCurrentHolder().setEnableCacheIp(true);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "允许持久化缓存");
}, "不允许持久化缓存", v -> {
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "鍏佽鎸佷箙鍖栫紦瀛?);
}, "涓嶅厑璁告寔涔呭寲缂撳瓨", v -> {
MyApp.getInstance().getCurrentHolder().setEnableCacheIp(false);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "不允许持久化缓存");
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "涓嶅厑璁告寔涔呭寲缂撳瓨");
});
addThreeButton("设置中国大陆", v -> {
addThreeButton("璁剧疆涓浗澶ч檰", v -> {
MyApp.getInstance().getCurrentHolder().setRegion(null);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "切换到中国大陆");
}, "设置中国香港", v -> {
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "鍒囨崲鍒颁腑鍥藉ぇ闄?);
}, "璁剧疆涓浗棣欐腐", v -> {
MyApp.getInstance().getCurrentHolder().setRegion("hk");
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "切换到中国香港");
}, "设置新加坡", v -> {
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "鍒囨崲鍒颁腑鍥介?);
}, "璁剧疆鏂板姞鍧?, v -> {
MyApp.getInstance().getCurrentHolder().setRegion("sg");
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "切换到新加坡");
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "鍒囨崲鍒版柊鍔犲潯");
});
addEditTextButton("超时时长 ms", "设置超时ms", view -> {
addEditTextButton("瓒呮椂鏃堕暱 ms", "璁剧疆瓒呮椂ms", view -> {
EditText et = (EditText) view;
int timeout = Integer.parseInt(et.getEditableText().toString());
MyApp.getInstance().getCurrentHolder().setTimeout(timeout);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "设置超时 " + timeout);
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "璁剧疆瓒呮椂 " + timeout);
});
addTwoButton("开启降级", v -> {
// 注意降级过滤器现在需要通过InitConfig设置重启应用生效
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "降级功能需要通过InitConfig配置");
}, "关闭降级", v -> {
// 注意降级过滤器现在需要通过InitConfig设置重启应用生效
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "降级功能需要通过InitConfig配置");
addTwoButton("檷绾?, v -> {
// 娉ㄦ剰锛氶檷绾ц繃婊ゅ櫒鐜板湪闇€瑕侀€氳繃InitConfig璁剧疆锛岄噸鍚簲鐢ㄧ敓鏁?
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "闄嶇骇鍔熻兘闇€瑕侀€氳繃InitConfig閰嶇疆");
}, "鍏抽棴闄嶇骇", v -> {
// 娉ㄦ剰锛氶檷绾ц繃婊ゅ櫒鐜板湪闇€瑕侀€氳繃InitConfig璁剧疆锛岄噸鍚簲鐢ㄧ敓鏁?
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "闄嶇骇鍔熻兘闇€瑕侀€氳繃InitConfig閰嶇疆");
});
addTwoButton("开启网络变化后预解析", v -> {
// 注意此功能现在需要通过InitConfig设置重启应用生效
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "网络变化后预解析需要通过InitConfig配置");
}, "关闭网络变化后预解析", v -> {
// 注意此功能现在需要通过InitConfig设置重启应用生效
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "网络变化后预解析需要通过InitConfig配置");
addTwoButton("寮€鍚綉缁滃彉鍖栧悗棰勮В鏋?, v -> {
// 娉ㄦ剰锛氭鍔熻兘鐜板湪闇€瑕侀€氳繃InitConfig璁剧疆锛岄噸鍚簲鐢ㄧ敓鏁?
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "缃戠粶鍙樺寲鍚庨瑙f瀽闇瑕侀氳繃InitConfig閰嶇疆");
}, "鍏抽棴缃戠粶鍙樺寲鍚庨瑙f瀽", v -> {
// 娉ㄦ剰锛氭鍔熻兘鐜板湪闇€瑕侀€氳繃InitConfig璁剧疆锛岄噸鍚簲鐢ㄧ敓鏁?
sendLog(MyApp.getInstance().getCurrentHolder().getAccountId() + "缃戠粶鍙樺寲鍚庨瑙f瀽闇瑕侀氳繃InitConfig閰嶇疆");
});
addView(R.layout.item_autocomplete_edittext_button, view -> {
@@ -147,21 +147,21 @@ public class HttpDnsActivity extends BaseActivity {
final EditText etOne = view.findViewById(R.id.etOne);
Button btnOne = view.findViewById(R.id.btnOne);
actvOne.setHint("请输入域名");
actvOne.setHint("璇疯緭鍏ュ煙鍚?);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_dropdown_item_1line, hosts);
actvOne.setAdapter(adapter);
etOne.setHint("请输入自定义ttl");
etOne.setHint("璇疯緭鍏ヨ嚜瀹氫箟ttl");
btnOne.setText("指定域名ttl s");
btnOne.setText("鎸囧畾鍩熷悕ttl s");
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String host = actvOne.getEditableText().toString();
int ttl = Integer.parseInt(etOne.getEditableText().toString());
MyApp.getInstance().getCurrentHolder().setHostTtl(host, ttl);
sendLog("指定域名" + host + "的ttl为" + ttl + "");
sendLog("鎸囧畾鍩熷悕" + host + "鐨則tl涓? + ttl + "?);
}
});
});
@@ -171,124 +171,124 @@ public class HttpDnsActivity extends BaseActivity {
final EditText etOne = view.findViewById(R.id.etOne);
Button btnOne = view.findViewById(R.id.btnOne);
actvOne.setHint("域名");
actvOne.setHint("鍩熷悕");
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),
android.R.layout.simple_dropdown_item_1line, hosts);
actvOne.setAdapter(adapter);
etOne.setHint("请输入端口");
etOne.setHint("璇疯緭鍏ョ鍙?);
btnOne.setText("添加ipRanking配置");
btnOne.setText("娣诲姞ipRanking閰嶇疆");
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String host = actvOne.getEditableText().toString();
int port = Integer.parseInt(etOne.getEditableText().toString());
MyApp.getInstance().getCurrentHolder().addIpProbeItem(new IPRankingBean(host, port));
sendLog("添加域名" + host + " 探测");
sendLog("娣诲姞鍩熷悕" + host + " ");
}
});
});
addAutoCompleteTextViewButton(hosts, "主站域名", "添加主站域名", view -> {
addAutoCompleteTextViewButton(hosts, "涓荤珯鍩熷悕", "娣诲姞涓荤珯鍩熷悕", view -> {
AutoCompleteTextView actvOne = (AutoCompleteTextView) view;
String host = actvOne.getEditableText().toString();
MyApp.getInstance().getCurrentHolder().addHostWithFixedIp(host);
sendLog("添加主站域名" + host);
sendLog("娣诲姞涓荤珯鍩熷悕" + host);
});
addAutoCompleteTextViewButton(hosts, "域名", "删除指定域名的缓存", view -> {
addAutoCompleteTextViewButton(hosts, "鍩熷悕", "鍒犻櫎鎸囧畾鍩熷悕鐨勭紦瀛?, view -> {
AutoCompleteTextView actvOne = (AutoCompleteTextView) view;
String host = actvOne.getEditableText().toString();
ArrayList<String> list = new ArrayList<>();
list.add(host);
MyApp.getInstance().getService().cleanHostCache(list);
sendLog("清除指定host的缓存" + host);
sendLog("娓呴櫎鎸囧畾host鐨勭紦瀛? + host);
});
addOneButton("清除所有缓存", v -> {
addOneButton("娓呴櫎鎵鏈夌紦瀛?, v -> {
MyApp.getInstance().getService().cleanHostCache(null);
sendLog("清除所有缓存");
sendLog("娓呴櫎鎵€鏈夌紦瀛?);
});
addFourButton("获取当前网络状态", v -> {
addFourButton("鑾峰彇褰撳墠缃戠粶鐘舵?, v -> {
NetType type = HttpDnsNetworkDetector.getInstance().getNetType(getApplicationContext());
sendLog("获取网络状态 " + type.name());
}, "禁用网络状态缓存", v -> {
sendLog("鑾峰彇缃戠粶鐘舵€?" + type.name());
}, "绂佺敤缃戠粶鐘舵€佺紦瀛?, v -> {
HttpDnsNetworkDetector.getInstance().disableCache(true);
sendLog("网络状态 禁用缓存 ");
}, "开启网络状态缓存", v -> {
sendLog("缃戠粶鐘舵?绂佺敤缂撳瓨 ");
}, "綉缁滅姸鎬佺紦瀛?, v -> {
HttpDnsNetworkDetector.getInstance().disableCache(false);
sendLog("网络状态 开启缓存 ");
}, "清除网络状态缓存", v -> {
sendLog("缃戠粶鐘舵€?寮€鍚紦瀛?");
}, "娓呴櫎缃戠粶鐘舵€佺紦瀛?, v -> {
HttpDnsNetworkDetector.getInstance().cleanCache(false);
sendLog("网络状态清除缓存 ");
sendLog("缃戠粶鐘舵佹竻闄ょ紦瀛?");
});
addTwoButton("禁止读取IP", v -> {
addTwoButton("绂佹璇诲彇IP", v -> {
HttpDnsNetworkDetector.getInstance().setCheckInterface(false);
sendLog("查询网络状态时 禁止读取IP");
}, "允许读取IP", v -> {
sendLog("鏌ヨ缃戠粶鐘舵佹椂 绂佹璇诲彇IP");
}, "鍏佽璇诲彇IP", v -> {
HttpDnsNetworkDetector.getInstance().setCheckInterface(true);
sendLog("查询网络状态时 允许读取IP");
sendLog("鏌ヨ缃戠粶鐘舵佹椂 鍏佽璇诲彇IP");
});
addAutoCompleteTextViewButton(hosts, "域名", "设置检测网络使用的域名", view -> {
addAutoCompleteTextViewButton(hosts, "鍩熷悕", "璁剧疆妫娴嬬綉缁滀娇鐢ㄧ殑鍩熷悕", view -> {
AutoCompleteTextView actvOne = (AutoCompleteTextView) view;
String host = actvOne.getEditableText().toString();
HttpDnsNetworkDetector.getInstance().setHostToCheckNetType(host);
sendLog("设置检测网络状态使用的域名为" + host);
sendLog("璁剧疆妫娴嬬綉缁滅姸鎬佷娇鐢ㄧ殑鍩熷悕涓? + host);
});
addTwoButton("模拟请求使用https请求", v -> {
addTwoButton("妯℃嫙璇锋眰浣跨敤https璇锋眰", v -> {
schema = SCHEMA_HTTPS;
sendLog("测试url使用https");
}, "模拟请求使用http请求", v -> {
sendLog("娴嬭瘯url浣跨敤https");
}, "妯℃嫙璇锋眰浣跨敤http璇锋眰", v -> {
schema = SCHEMA_HTTP;
sendLog("测试url使用http");
sendLog("娴嬭瘯url浣跨敤http");
});
addTwoButton("HttpUrlConnection", v -> {
networkRequest = httpUrlConnectionRequest;
sendLog("指定网络实现方式为HttpUrlConnection");
sendLog("鎸囧畾缃戠粶瀹炵幇鏂瑰紡涓篐ttpUrlConnection");
}, "Okhttp", v -> {
networkRequest = okHttpRequest;
sendLog("指定网络实现方式为okhttp");
sendLog("鎸囧畾缃戠粶瀹炵幇鏂瑰紡涓簅khttp");
});
addFourButton("指定v4", v -> {
addFourButton("鎸囧畾v4", v -> {
requestIpType = RequestIpType.v4;
sendLog("要解析的IP类型指定为ipv4");
}, "指定v6", v -> {
sendLog("瑕佽В鏋愮殑IP绫诲瀷鎸囧畾涓篿pv4");
}, "鎸囧畾v6", v -> {
requestIpType = RequestIpType.v6;
sendLog("要解析的IP类型指定为ipv6");
}, "都解析", v -> {
sendLog("瑕佽В鏋愮殑IP绫诲瀷鎸囧畾涓篿pv6");
}, "閮借В鏋?, v -> {
requestIpType = RequestIpType.both;
sendLog("要解析的IP类型指定为ipv4和ipv6");
}, "自动判断", v -> {
sendLog("瑕佽В鏋愮殑IP绫诲瀷鎸囧畾涓篿pv4鍜宨pv6");
}, "姩鍒ゆ柇", v -> {
requestIpType = RequestIpType.auto;
sendLog("要解析的IP类型根据网络情况自动判断");
sendLog("瑕佽В鏋愮殑IP绫诲瀷鏍规嵁缃戠粶鎯呭喌鑷姩鍒ゆ柇");
});
addAutoCompleteTextViewButton(hosts, "域名", "指定要解析的域名", new OnButtonClick() {
addAutoCompleteTextViewButton(hosts, "鍩熷悕", "鎸囧畾瑕佽В鏋愮殑鍩熷悕", new OnButtonClick() {
@Override
public void onBtnClick(View view) {
AutoCompleteTextView actvOne = (AutoCompleteTextView) view;
host = actvOne.getEditableText().toString();
sendLog("要解析的域名" + host);
sendLog("瑕佽В鏋愮殑鍩熷悕" + host);
}
});
addTwoButton("异步解析", v -> worker.execute(new Runnable() {
addTwoButton("寮傛瑙f瀽", v -> worker.execute(new Runnable() {
@Override
public void run() {
sendLog("开始发起网络请求");
sendLog("网络实现方式为" + (networkRequest == httpUrlConnectionRequest ? "HttpUrlConnection" : "okhttp"));
sendLog("濮嬪彂璧风綉缁滆?);
sendLog("缃戠粶瀹炵幇鏂瑰紡涓? + (networkRequest == httpUrlConnectionRequest ? "HttpUrlConnection" : "okhttp"));
String url = getUrl(schema, host);
sendLog("url is " + url);
sendLog("httpdns 使用 异步解析api");
sendLog("指定解析ip类型为" + requestIpType.name());
sendLog("httpdns 浣跨敤 寮傛瀽api");
sendLog("鎸囧畾瑙瀽ip绫诲瀷涓? + requestIpType.name());
networkRequest.updateHttpDnsConfig(true, requestIpType);
try {
String response = networkRequest.httpGet(url);
@@ -298,40 +298,40 @@ public class HttpDnsActivity extends BaseActivity {
response = response.substring(0, 30) + "...";
}
}
sendLog("请求结束 response is " + response + " 完整记录请看logcat日志");
sendLog("璇锋眰缁撴潫 response is " + response + " 瀹屾暣璁板綍璇风湅logcat鏃ュ織");
} catch (Exception e) {
e.printStackTrace();
sendLog("请求结束 发生异常 " + e.getClass().getName() + e.getMessage() + " 完整记录请看logcat日志");
sendLog("璇锋眰缁撴潫 鍙戠敓寮傚父 " + e.getClass().getName() + e.getMessage() + " 瀹屾暣璁板綍璇风湅logcat鏃ュ織");
}
}
}), "同步解析", v -> worker.execute(() -> {
sendLog("开始发起网络请求");
sendLog("网络实现方式为" + (networkRequest == httpUrlConnectionRequest ? "HttpUrlConnection" : "okhttp"));
}), "鍚屾瑙f瀽", v -> worker.execute(() -> {
sendLog("寮€濮嬪彂璧风綉缁滆姹?);
sendLog("缃戠粶瀹炵幇鏂瑰紡涓? + (networkRequest == httpUrlConnectionRequest ? "HttpUrlConnection" : "okhttp"));
String url = getUrl(schema, host);
sendLog("url is " + url);
sendLog("httpdns 使用 同步解析api");
sendLog("指定解析ip类型为" + requestIpType.name());
sendLog("httpdns 浣跨敤 鍚屾瑙f瀽api");
sendLog("鎸囧畾瑙瀽ip绫诲瀷涓? + requestIpType.name());
networkRequest.updateHttpDnsConfig(false, requestIpType);
try {
String response = networkRequest.httpGet(url);
if (response != null && response.length() > 30) {
response = response.substring(0, 30) + "...";
}
sendLog("请求结束 response is " + response + " 完整记录请看logcat日志");
sendLog("璇锋眰缁撴潫 response is " + response + " 瀹屾暣璁板綍璇风湅logcat鏃ュ織");
} catch (Exception e) {
e.printStackTrace();
sendLog("请求结束 发生异常 " + e.getClass().getName() + e.getMessage() + " 完整记录请看logcat日志");
sendLog("璇锋眰缁撴潫 鍙戠敓寮傚父 " + e.getClass().getName() + e.getMessage() + " 瀹屾暣璁板綍璇风湅logcat鏃ュ織");
}
}));
addThreeButton("发起预解析", v -> worker.execute(() -> {
addThreeButton("鍙戣捣棰勮В鏋?, v -> worker.execute(() -> {
ArrayList<String> tmp = new ArrayList<>();
Collections.addAll(tmp, hosts);
MyApp.getInstance().getService().setPreResolveHosts(tmp, requestIpType);
sendLog("已发起预解析请求");
}), "跳转SDNS测试界面",
v -> startActivity(new Intent(HttpDnsActivity.this, SDNSActivity.class)), "跳转Webview测试界面", new View.OnClickListener() {
sendLog("宸插彂璧烽瑙f瀽璇锋眰");
}), "璺宠浆SDNS娴嬭瘯鐣岄潰",
v -> startActivity(new Intent(HttpDnsActivity.this, SDNSActivity.class)), "璺宠浆Webview娴嬭瘯鐣岄潰", new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(HttpDnsActivity.this, WebViewActivity.class));
@@ -340,17 +340,18 @@ public class HttpDnsActivity extends BaseActivity {
final String[] validHosts = new String[]{
"www.aliyun.com",
"www.Aliyun.com",
"www.taobao.com"
};
addTwoButton("并发异步请求", v -> {
addTwoButton("骞跺彂寮傛璇锋眰", v -> {
ThreadUtil.multiThreadTest(validHosts, 100, 20, 10 * 60 * 1000, true, requestIpType);
sendLog("异步api并发测试开始大约耗时10分钟请最后查看logcat日志确认结果建议关闭httpdns日志避免日志量过大");
}, "并发同步请求", v -> {
sendLog("寮傛api骞跺彂娴嬭瘯寮€濮嬶紝澶х害鑰楁椂10鍒嗛挓锛岃鏈€鍚庢煡鐪媗ogcat鏃ュ織锛岀璁ょ粨鏋滐紝寤鸿鍏抽棴httpdns鏃ュ織锛岄伩鍏嶆棩蹇楅噺杩囧ぇ");
}, "骞跺彂鍚屾璇锋眰", v -> {
ThreadUtil.multiThreadTest(validHosts, 100, 20, 10 * 60 * 1000, false, requestIpType);
sendLog("同步api并发测试开始大约耗时10分钟请最后查看logcat日志确认结果建议关闭httpdns日志避免日志量过大");
sendLog("鍚屾api骞跺彂娴嬭瘯寮€濮嬶紝澶х害鑰楁椂10鍒嗛挓锛岃鏈€鍚庢煡鐪媗ogcat鏃ュ織锛岀璁ょ粨鏋滐紝寤鸿鍏抽棴httpdns鏃ュ織锛岄伩鍏嶆棩蹇楅噺杩囧ぇ");
});
}
}

View File

@@ -21,8 +21,8 @@ import java.util.Iterator;
import java.util.List;
/**
* 保存Httpdns 及 相关配置,
* 方便修改
* 淇濆瓨Httpdns 鍙?鐩稿叧閰嶇疆锛?
* 鏂逛究淇敼
*/
public class HttpDnsHolder {
@@ -74,7 +74,7 @@ public class HttpDnsHolder {
}
/**
* 初始化httpdns的配置
* 鍒濆鍖杊ttpdns鐨勯厤缃?
*
* @param context
*/
@@ -94,7 +94,7 @@ public class HttpDnsHolder {
}
});
// 初始化httpdns 的配置此步骤需要在第一次获取HttpDnsService实例之前
// 鍒濆鍖杊ttpdns 鐨勯厤缃紝姝ゆ楠ら渶瑕佸湪绗竴娆¤幏鍙朒ttpDnsService瀹炰緥涔嬪墠
new InitConfig.Builder()
.setEnableExpiredIp(enableExpiredIp)
.setEnableCacheIp(enableCacheIp)
@@ -126,7 +126,7 @@ public class HttpDnsHolder {
public void setEnableExpiredIp(final boolean enableExpiredIp) {
this.enableExpiredIp = enableExpiredIp;
// 注意:此配置需要重启应用生效,因为现在通过InitConfig设置
// 娉ㄦ剰锛氭閰嶇疆闇€瑕侀噸鍚簲鐢ㄧ敓鏁堬紝鍥犱负鐜板湪閫氳繃InitConfig璁剧疆
SpUtil.writeSp(context, getSpName(accountId), new SpUtil.OnGetSpEditor() {
@Override
public void onGetSpEditor(SharedPreferences.Editor editor) {
@@ -137,7 +137,7 @@ public class HttpDnsHolder {
public void setEnableCacheIp(final boolean enableCacheIp) {
this.enableCacheIp = enableCacheIp;
// 注意:此配置需要重启应用生效,因为现在通过InitConfig设置
// 娉ㄦ剰锛氭閰嶇疆闇€瑕侀噸鍚簲鐢ㄧ敓鏁堬紝鍥犱负鐜板湪閫氳繃InitConfig璁剧疆
SpUtil.writeSp(context, getSpName(accountId), new SpUtil.OnGetSpEditor() {
@Override
public void onGetSpEditor(SharedPreferences.Editor editor) {
@@ -148,7 +148,7 @@ public class HttpDnsHolder {
public void setTimeout(final int timeout) {
this.timeout = timeout;
// 注意:此配置需要重启应用生效,因为现在通过InitConfig设置
// 娉ㄦ剰锛氭閰嶇疆闇€瑕侀噸鍚簲鐢ㄧ敓鏁堬紝鍥犱负鐜板湪閫氳繃InitConfig璁剧疆
SpUtil.writeSp(context, getSpName(accountId), new SpUtil.OnGetSpEditor() {
@Override
public void onGetSpEditor(SharedPreferences.Editor editor) {
@@ -159,7 +159,7 @@ public class HttpDnsHolder {
public void setEnableHttps(final boolean enableHttps) {
this.enableHttps = enableHttps;
// 注意:此配置需要重启应用生效,因为现在通过InitConfig设置
// 娉ㄦ剰锛氭閰嶇疆闇€瑕侀噸鍚簲鐢ㄧ敓鏁堬紝鍥犱负鐜板湪閫氳繃InitConfig璁剧疆
SpUtil.writeSp(context, getSpName(accountId), new SpUtil.OnGetSpEditor() {
@Override
public void onGetSpEditor(SharedPreferences.Editor editor) {
@@ -184,7 +184,7 @@ public class HttpDnsHolder {
this.hostListWithFixedIp = new ArrayList<>();
}
this.hostListWithFixedIp.add(host);
// 重启生效
// 閲嶅惎鐢熸晥
SpUtil.writeSp(context, getSpName(accountId), new SpUtil.OnGetSpEditor() {
@Override
public void onGetSpEditor(SharedPreferences.Editor editor) {
@@ -198,7 +198,7 @@ public class HttpDnsHolder {
this.ipRankingList = new ArrayList<>();
}
this.ipRankingList.add(ipProbeItem);
// 注意:此配置需要重启应用生效,因为现在通过InitConfig设置
// 娉ㄦ剰锛氭閰嶇疆闇€瑕侀噸鍚簲鐢ㄧ敓鏁堬紝鍥犱负鐜板湪閫氳繃InitConfig璁剧疆
SpUtil.writeSp(context, getSpName(accountId), new SpUtil.OnGetSpEditor() {
@Override
public void onGetSpEditor(SharedPreferences.Editor editor) {
@@ -232,15 +232,15 @@ public class HttpDnsHolder {
public String getCurrentConfig() {
StringBuilder sb = new StringBuilder();
sb.append("当前配置 accountId : ").append(accountId).append("\n")
.append("是否允许过期IP : ").append(enableExpiredIp).append("\n")
.append("是否开启本地缓存 : ").append(enableCacheIp).append("\n")
.append("是否开启HTTPS : ").append(enableHttps).append("\n")
.append("当前region设置 : ").append(region).append("\n")
.append("当前超时设置 : ").append(timeout).append("\n")
.append("当前探测配置 : ").append(convertProbeList(ipRankingList)).append("\n")
.append("当前缓存配置 : ").append(convertTtlCache(ttlCache)).append("\n")
.append("当前主站域名配置 : ").append(convertHostList(hostListWithFixedIp)).append("\n")
sb.append("褰撳墠閰嶇疆 accountId : ").append(accountId).append("\n")
.append("鏄惁鍏佽杩囨湡IP : ").append(enableExpiredIp).append("\n")
.append("鏄惁寮€鍚湰鍦扮紦瀛?: ").append(enableCacheIp).append("\n")
.append("鏄惁寮€鍚疕TTPS : ").append(enableHttps).append("\n")
.append("褰撳墠region璁剧疆 : ").append(region).append("\n")
.append("褰撳墠瓒呮椂璁剧疆 : ").append(timeout).append("\n")
.append("褰撳墠鎺㈡祴閰嶇疆 : ").append(convertProbeList(ipRankingList)).append("\n")
.append("褰撳墠缂撳瓨閰嶇疆 : ").append(convertTtlCache(ttlCache)).append("\n")
.append("褰撳墠涓荤珯鍩熷悕閰嶇疆 : ").append(convertHostList(hostListWithFixedIp)).append("\n")
;
return sb.toString();
}
@@ -340,3 +340,4 @@ public class HttpDnsHolder {
return null;
}
}

View File

@@ -23,8 +23,8 @@ public class MyApp extends Application {
return instance;
}
private final HttpDnsHolder holderA = new HttpDnsHolder("请替换为测试用A实例的accountId", "请替换为测试用A实例的secret");
private final HttpDnsHolder holderB = new HttpDnsHolder("请替换为测试用B实例的accountId", null);
private final HttpDnsHolder holderA = new HttpDnsHolder("璇锋浛鎹负娴嬭瘯鐢ˋ瀹炰緥鐨刟ccountId", "璇锋浛鎹负娴嬭瘯鐢ˋ瀹炰緥鐨剆ecret");
private final HttpDnsHolder holderB = new HttpDnsHolder("璇锋浛鎹负娴嬭瘯鐢˙瀹炰緥鐨刟ccountId", null);
private HttpDnsHolder current = holderA;
@@ -33,9 +33,9 @@ public class MyApp extends Application {
super.onCreate();
instance = this;
// 开启logcat 日志 默认关闭, 开发测试过程中可以开启
// 寮€鍚痩ogcat 鏃ュ織 榛樿鍏抽棴, 寮€鍙戞祴璇曡繃绋嬩腑鍙互寮€鍚?
HttpDnsLog.enable(true);
// 注入日志接口接受httpdns的日志开发测试过程中可以开启, 基础日志需要先enable才生效一些错误日志不需要
// 娉ㄥ叆鏃ュ織鎺ュ彛锛屾帴鍙梙ttpdns鐨勬棩蹇楋紝寮€鍙戞祴璇曡繃绋嬩腑鍙互寮€鍚? 鍩虹鏃ュ織闇€瑕佸厛enable鎵嶇敓鏁堬紝涓€浜涢敊璇棩蹇椾笉闇€瑕?
HttpDnsLog.setLogger(new ILogger() {
@Override
public void log(String msg) {
@@ -43,7 +43,7 @@ public class MyApp extends Application {
}
});
// 初始化httpdns的配置
// 鍒濆鍖杊ttpdns鐨勯厤缃?
holderA.init(this);
holderB.init(this);
@@ -90,3 +90,4 @@ public class MyApp extends Application {
}
}

View File

@@ -5,12 +5,12 @@ import com.alibaba.sdk.android.httpdns.RequestIpType;
public interface NetworkRequest {
/**
* 设置httpdns的配置
* 璁剧疆httpdns鐨勯厤缃?
*/
void updateHttpDnsConfig(boolean async, RequestIpType requestIpType);
/**
* get请求
* get璇锋眰
*
* @param url
* @return
@@ -18,3 +18,4 @@ public interface NetworkRequest {
String httpGet(String url) throws Exception;
}

View File

@@ -11,17 +11,17 @@ import com.aliyun.ams.httpdns.demo.base.BaseActivity;
import java.util.HashMap;
import static com.aliyun.ams.httpdns.demo.HttpDnsActivity.ALIYUN_URL;
import static com.aliyun.ams.httpdns.demo.HttpDnsActivity.Aliyun_URL;
public class SDNSActivity extends BaseActivity {
private final HashMap<String, String> globalParams = new HashMap<>();
/**
* 要请求的域名
* 瑕佽姹傜殑鍩熷悕
*/
private String host = HttpDnsActivity.ALIYUN_URL;
private String host = HttpDnsActivity.Aliyun_URL;
/**
* 要解析的ip类型
* 瑕佽В鏋愮殑ip绫诲瀷
*/
private RequestIpType requestIpType = RequestIpType.v4;
@@ -29,7 +29,7 @@ public class SDNSActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addEditTextEditTextButton("key", "value", "添加全局配置", new OnButtonClickMoreView() {
addEditTextEditTextButton("key", "value", "娣诲姞鍏ㄥ眬閰嶇疆", new OnButtonClickMoreView() {
@Override
public void onBtnClick(View[] views) {
EditText etOne = (EditText) views[0];
@@ -40,56 +40,56 @@ public class SDNSActivity extends BaseActivity {
globalParams.put(key, value);
// 注意SDNS全局参数现在需要通过InitConfig设置重启应用生效
sendLog("添加全局参数 " + key + " : " + value);
// 娉ㄦ剰锛歋DNS鍏ㄥ眬鍙傛暟鐜板湪闇€瑕侀€氳繃InitConfig璁剧疆锛岄噸鍚簲鐢ㄧ敓鏁?
sendLog("娣诲姞鍏ㄥ眬鍙傛暟 " + key + " : " + value);
}
});
addOneButton("清除全局配置", new View.OnClickListener() {
addOneButton("娓呴櫎鍏ㄥ眬閰嶇疆", new View.OnClickListener() {
@Override
public void onClick(View v) {
globalParams.clear();
// 注意SDNS全局参数现在需要通过InitConfig设置重启应用生效
sendLog("清除全局参数");
// 娉ㄦ剰锛歋DNS鍏ㄥ眬鍙傛暟鐜板湪闇€瑕侀€氳繃InitConfig璁剧疆锛岄噸鍚簲鐢ㄧ敓鏁?
sendLog("娓呴櫎鍏ㄥ眬鍙傛暟");
}
});
addFourButton("指定v4", new View.OnClickListener() {
addFourButton("鎸囧畾v4", new View.OnClickListener() {
@Override
public void onClick(View v) {
requestIpType = RequestIpType.v4;
sendLog("要解析的IP类型指定为ipv4");
sendLog("瑕佽В鏋愮殑IP绫诲瀷鎸囧畾涓篿pv4");
}
}, "指定v6", new View.OnClickListener() {
}, "鎸囧畾v6", new View.OnClickListener() {
@Override
public void onClick(View v) {
requestIpType = RequestIpType.v6;
sendLog("要解析的IP类型指定为ipv6");
sendLog("瑕佽В鏋愮殑IP绫诲瀷鎸囧畾涓篿pv6");
}
}, "都解析", new View.OnClickListener() {
}, "閮借В鏋?, new View.OnClickListener() {
@Override
public void onClick(View v) {
requestIpType = RequestIpType.both;
sendLog("要解析的IP类型指定为ipv4和ipv6");
sendLog("瑕佽В鏋愮殑IP绫诲瀷鎸囧畾涓篿pv4鍜宨pv6");
}
}, "自动判断", new View.OnClickListener() {
}, "姩鍒ゆ柇", new View.OnClickListener() {
@Override
public void onClick(View v) {
requestIpType = RequestIpType.auto;
sendLog("要解析的IP类型根据网络情况自动判断");
sendLog("瑕佽В鏋愮殑IP绫诲瀷鏍规嵁缃戠粶鎯呭喌鑷姩鍒ゆ柇");
}
});
addAutoCompleteTextViewButton(HttpDnsActivity.hosts, "域名", "指定要解析的域名", new OnButtonClick() {
addAutoCompleteTextViewButton(HttpDnsActivity.hosts, "鍩熷悕", "鎸囧畾瑕佽В鏋愮殑鍩熷悕", new OnButtonClick() {
@Override
public void onBtnClick(View view) {
AutoCompleteTextView actvOne = (AutoCompleteTextView) view;
host = actvOne.getEditableText().toString();
sendLog("要解析的域名" + host);
sendLog("瑕佽В鏋愮殑鍩熷悕" + host);
}
});
addEditTextEditTextButton("key", "value", "发起请求", new OnButtonClickMoreView() {
addEditTextEditTextButton("key", "value", "鍙戣捣璇锋眰", new OnButtonClickMoreView() {
@Override
public void onBtnClick(View[] views) {
EditText etOne = (EditText) views[0];
@@ -102,31 +102,32 @@ public class SDNSActivity extends BaseActivity {
map.put(key, value);
sendLog("发起SDNS请求 requestIpType is " + requestIpType.name());
HTTPDNSResult result = MyApp.getInstance().getService().getIpsByHostAsync(host, requestIpType, map, "测试SDNS");
sendLog("结果 " + result);
sendLog("鍙戣捣SDNS璇锋眰 requestIpType is " + requestIpType.name());
HTTPDNSResult result = MyApp.getInstance().getService().getIpsByHostAsync(host, requestIpType, map, "娴嬭瘯SDNS");
sendLog("缁撴灉 " + result);
}
});
addTwoButton("scale1参数请求", new View.OnClickListener() {
addTwoButton("scale1鍙傛暟璇锋眰", new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String, String> map = new HashMap<>();
map.put("scale", "scale1");
sendLog("发起SDNS请求 requestIpType is " + requestIpType.name() + " scale : scale1");
HTTPDNSResult result = MyApp.getInstance().getService().getIpsByHostAsync(host, requestIpType, map, "测试1");
sendLog("结果 " + result);
sendLog("鍙戣捣SDNS璇锋眰 requestIpType is " + requestIpType.name() + " scale : scale1");
HTTPDNSResult result = MyApp.getInstance().getService().getIpsByHostAsync(host, requestIpType, map, "娴嬭瘯1");
sendLog("缁撴灉 " + result);
}
}, "scale2参数请求", new View.OnClickListener() {
}, "scale2鍙傛暟璇锋眰", new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String, String> map = new HashMap<>();
map.put("scale", "scale2");
sendLog("发起SDNS请求 requestIpType is " + requestIpType.name() + " scale : scale2");
HTTPDNSResult result = MyApp.getInstance().getService().getIpsByHostAsync(host, requestIpType, map, "测试2");
sendLog("结果 " + result);
sendLog("鍙戣捣SDNS璇锋眰 requestIpType is " + requestIpType.name() + " scale : scale2");
HTTPDNSResult result = MyApp.getInstance().getService().getIpsByHostAsync(host, requestIpType, map, "娴嬭瘯2");
sendLog("缁撴灉 " + result);
}
});
}
}

View File

@@ -55,10 +55,10 @@ public class WebViewActivity extends Activity {
public boolean
onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();//返回上个页面
webView.goBack();//杩斿洖涓婁釜椤甸潰
return true;
}
return super.onKeyDown(keyCode, event);//退出Activity
return super.onKeyDown(keyCode, event);//閫€鍑篈ctivity
}
private void initBar() {
@@ -86,7 +86,7 @@ public class WebViewActivity extends Activity {
Map<String, String> headerFields = request.getRequestHeaders();
String url = request.getUrl().toString();
Log.e(TAG, "url:" + url);
// 无法拦截body拦截方案只能正常处理不带body的请求
// 鏃犳硶鎷︽埅body锛屾嫤鎴柟妗堝彧鑳芥甯稿鐞嗕笉甯ody鐨勮姹傦紱
if ((scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))
&& method.equalsIgnoreCase("get")) {
try {
@@ -97,7 +97,7 @@ public class WebViewActivity extends Activity {
return super.shouldInterceptRequest(view, request);
}
// 注*对于POST请求的Body数据WebResourceRequest接口中并没有提供这里无法处理
// 娉?锛氬浜嶱OST璇锋眰鐨凚ody鏁版嵁锛學ebResourceRequest鎺ュ彛涓苟娌℃湁鎻愪緵锛岃繖閲屾棤娉曞鐞?
String contentType = connection.getContentType();
String mime = getMime(contentType);
String charset = getCharset(contentType);
@@ -110,18 +110,18 @@ public class WebViewActivity extends Activity {
Log.e(TAG, "mime:" + mime + "; charset:" + charset);
// 无mime类型的请求不拦截
// 鏃爉ime绫诲瀷鐨勮姹備笉鎷︽埅
if (TextUtils.isEmpty(mime)) {
Log.e(TAG, "no MIME");
return super.shouldInterceptRequest(view, request);
} else {
// 二进制资源无需编码信息
// 浜岃繘鍒惰祫婧愭棤闇€缂栫爜淇℃伅
if (!TextUtils.isEmpty(charset) || (isBinaryRes(mime))) {
WebResourceResponse resourceResponse = new WebResourceResponse(mime, charset, httpURLConnection.getInputStream());
resourceResponse.setStatusCodeAndReasonPhrase(statusCode, response);
Map<String, String> responseHeader = new HashMap<String, String>();
for (String key : headerKeySet) {
// HttpUrlConnection可能包含key为null的报头指向该http请求状态码
// HttpUrlConnection鍙兘鍖呭惈key涓簄ull鐨勬姤澶达紝鎸囧悜璇ttp璇锋眰鐘舵€佺爜
responseHeader.put(key, httpURLConnection.getHeaderField(key));
}
resourceResponse.setResponseHeaders(responseHeader);
@@ -142,7 +142,7 @@ public class WebViewActivity extends Activity {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// API < 21 只能拦截URL参数
// API < 21 鍙兘鎷︽埅URL鍙傛暟
return super.shouldInterceptRequest(view, url);
}
});
@@ -153,7 +153,7 @@ public class WebViewActivity extends Activity {
/**
* 从contentType中获取MIME类型
* 浠巆ontentType涓幏鍙朚IME绫诲瀷
*
* @param contentType
* @return
@@ -166,7 +166,7 @@ public class WebViewActivity extends Activity {
}
/**
* 从contentType中获取编码信息
* 浠巆ontentType涓幏鍙栫紪鐮佷俊鎭?
*
* @param contentType
* @return
@@ -191,7 +191,7 @@ public class WebViewActivity extends Activity {
/**
* 是否是二进制资源,二进制资源可以不需要编码信息
* 鏄惁鏄簩杩涘埗璧勬簮锛屼簩杩涘埗璧勬簮鍙互涓嶉渶瑕佺紪鐮佷俊鎭?
*/
private boolean isBinaryRes(String mime) {
if (mime.startsWith("image")
@@ -209,10 +209,10 @@ public class WebViewActivity extends Activity {
URL url = null;
try {
url = new URL(path);
// 异步接口获取IP
// 寮傛鎺ュ彛鑾峰彇IP
String ip = MyApp.getInstance().getService().getIpByHostAsync(url.getHost());
if (ip != null) {
// 通过HTTPDNS获取IP成功进行URL替换和HOST头设置
// 閫氳繃HTTPDNS鑾峰彇IP鎴愬姛锛岃繘琛孶RL鏇挎崲鍜孒OST澶磋缃?
Log.d(TAG, "Get IP: " + ip + " for host: " + url.getHost() + " from HTTPDNS successfully!");
String newUrl = path.replaceFirst(url.getHost(), ip);
conn = (HttpURLConnection) new URL(newUrl).openConnection();
@@ -222,7 +222,7 @@ public class WebViewActivity extends Activity {
conn.setRequestProperty(field.getKey(), field.getValue());
}
}
// 设置HTTP请求头Host
// 璁剧疆HTTP璇锋眰澶碒ost鍩?
conn.setRequestProperty("Host", url.getHost());
} else {
return null;
@@ -235,9 +235,9 @@ public class WebViewActivity extends Activity {
WebviewTlsSniSocketFactory sslSocketFactory = new WebviewTlsSniSocketFactory(
(HttpsURLConnection)conn);
// sni场景创建SSLScocket
// sni鍦烘櫙锛屽垱寤篠SLScocket
httpsURLConnection.setSSLSocketFactory(sslSocketFactory);
// https场景,证书校验
// https鍦烘櫙锛岃瘉涔︽牎楠?
httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
@@ -251,7 +251,7 @@ public class WebViewActivity extends Activity {
}
int code = conn.getResponseCode();// Network block
if (needRedirect(code)) {
// 原有报头中含有cookie放弃拦截
// 鍘熸湁鎶ュご涓惈鏈塩ookie锛屾斁寮冩嫤鎴?
if (containCookie(headers)) {
return null;
}
@@ -264,7 +264,7 @@ public class WebViewActivity extends Activity {
if (location != null) {
if (!(location.startsWith("http://") || location
.startsWith("https://"))) {
//某些时候会省略host只返回后面的path所以需要补全url
//鏌愪簺鏃跺€欎細鐪佺暐host锛屽彧杩斿洖鍚庨潰鐨刾ath锛屾墍浠ラ渶瑕佽ˉ鍏rl
URL originalUrl = new URL(path);
location = originalUrl.getProtocol() + "://"
+ originalUrl.getHost() + location;
@@ -272,7 +272,7 @@ public class WebViewActivity extends Activity {
Log.e(TAG, "code: " + code + "; location: " + location + "; path " + path);
return recursiveRequest(location, headers, path);
} else {
// 无法获取location信息,让浏览器获取
// 鏃犳硶鑾峰彇location淇℃伅锛岃娴忚鍣ㄨ幏鍙?
return null;
}
} else {
@@ -297,7 +297,7 @@ public class WebViewActivity extends Activity {
/**
* header中是否含有cookie
* header涓槸鍚﹀惈鏈塩ookie
*/
private boolean containCookie(Map<String, String> headers) {
for (Map.Entry<String, String> headerField : headers.entrySet()) {
@@ -400,3 +400,4 @@ public class WebViewActivity extends Activity {
}
}
}

View File

@@ -66,7 +66,7 @@ public class BaseActivity extends Activity {
}
/**
* 发送日志到界面
* 鍙戦€佹棩蹇楀埌鐣岄潰
*
* @param log
*/
@@ -261,3 +261,4 @@ public class BaseActivity extends Activity {
void onBtnClick(View[] views);
}
}

View File

@@ -33,7 +33,7 @@ import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
/**
* 使用HttpUrlConnection 实现请求
* 浣跨敤HttpUrlConnection 瀹炵幇璇锋眰
*/
public class HttpUrlConnectionRequest implements NetworkRequest {
@@ -55,7 +55,7 @@ public class HttpUrlConnectionRequest implements NetworkRequest {
@Override
public String httpGet(String url) throws Exception {
Log.d(TAG, "使用httpUrlConnection 请求" + url + " 异步接口 " + async + " ip类型 " + type.name());
Log.d(TAG, "浣跨敤httpUrlConnection 璇锋眰" + url + " 寮傛鎺ュ彛 " + async + " ip绫诲瀷 " + type.name());
HttpURLConnection conn = getConnection(url);
InputStream in = null;
@@ -67,13 +67,13 @@ public class HttpUrlConnectionRequest implements NetworkRequest {
streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
errStr = readStringFrom(streamReader).toString();
}
Log.d(TAG, "请求失败 " + conn.getResponseCode() + " err " + errStr);
Log.d(TAG, "璇锋眰澶辫触 " + conn.getResponseCode() + " err " + errStr);
throw new Exception("Status Code : " + conn.getResponseCode() + " Msg : " + errStr);
} else {
in = conn.getInputStream();
streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String responseStr = readStringFrom(streamReader).toString();
Log.d(TAG, "请求成功 " + responseStr);
Log.d(TAG, "璇锋眰鎴愬姛 " + responseStr);
return responseStr;
}
}
@@ -82,29 +82,29 @@ public class HttpUrlConnectionRequest implements NetworkRequest {
final String host = new URL(url).getHost();
HttpURLConnection conn = null;
HTTPDNSResult result;
/* 切换为新版标准api */
/* 鍒囨崲涓烘柊鐗堟爣鍑哸pi */
if (async) {
result = MyApp.getInstance().getService().getHttpDnsResultForHostAsync(host, type);
} else {
result = MyApp.getInstance().getService().getHttpDnsResultForHostSync(host, type);
}
Log.d(TAG, "httpdns 解析 " + host + " 结果为 " + result + " ttl is " + Util.getTtl(result));
Log.d(TAG, "httpdns 瑙f瀽 " + host + " 缁撴灉涓?" + result + " ttl is " + Util.getTtl(result));
// 这里需要根据实际情况选择使用ipv6地址 还是 ipv4地址 下面示例的代码优先使用了ipv6地址
// 杩欓噷闇€瑕佹牴鎹疄闄呮儏鍐甸€夋嫨浣跨敤ipv6鍦板潃 杩樻槸 ipv4鍦板潃锛?涓嬮潰绀轰緥鐨勪唬鐮佷紭鍏堜娇鐢ㄤ簡ipv6鍦板潃
if (result.getIpv6s() != null && result.getIpv6s().length > 0 && HttpDnsNetworkDetector.getInstance().getNetType(context) != NetType.v4) {
String newUrl = url.replace(host, "[" + result.getIpv6s()[0] + "]");
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Host", host);
Log.d(TAG, "使用ipv6地址 " + newUrl);
Log.d(TAG, "浣跨敤ipv6鍦板潃 " + newUrl);
} else if (result.getIps() != null && result.getIps().length > 0 && HttpDnsNetworkDetector.getInstance().getNetType(context) != NetType.v6) {
String newUrl = url.replace(host, result.getIps()[0]);
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Host", host);
Log.d(TAG, "使用ipv4地址 " + newUrl);
Log.d(TAG, "浣跨敤ipv4鍦板潃 " + newUrl);
}
if (conn == null) {
Log.d(TAG, "httpdns 未返回解析结果走localdns");
Log.d(TAG, "httpdns 鏈繑鍥炶В鏋愮粨鏋滐紝璧發ocaldns");
conn = (HttpURLConnection) new URL(url).openConnection();
}
conn.setConnectTimeout(30000);
@@ -115,9 +115,9 @@ public class HttpUrlConnectionRequest implements NetworkRequest {
WebviewTlsSniSocketFactory sslSocketFactory = new WebviewTlsSniSocketFactory(
(HttpsURLConnection)conn);
// sni场景创建SSLSocket
// sni鍦烘櫙锛屽垱寤篠SLSocket
httpsURLConnection.setSSLSocketFactory(sslSocketFactory);
// https场景,证书校验
// https鍦烘櫙锛岃瘉涔︽牎楠?
httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
@@ -131,14 +131,14 @@ public class HttpUrlConnectionRequest implements NetworkRequest {
}
int code = conn.getResponseCode();// Network block
if (needRedirect(code)) {
//临时重定向和永久重定向location的大小写有区分
//涓存椂閲嶅畾鍚戝拰姘镐箙閲嶅畾鍚憀ocation鐨勫ぇ灏忓啓鏈夊尯鍒?
String location = conn.getHeaderField("Location");
if (location == null) {
location = conn.getHeaderField("location");
}
if (!(location.startsWith("http://") || location
.startsWith("https://"))) {
//某些时候会省略host只返回后面的path所以需要补全url
//鏌愪簺鏃跺€欎細鐪佺暐host锛屽彧杩斿洖鍚庨潰鐨刾ath锛屾墍浠ラ渶瑕佽ˉ鍏rl
URL originalUrl = new URL(url);
location = originalUrl.getProtocol() + "://"
+ originalUrl.getHost() + location;
@@ -255,3 +255,4 @@ public class HttpUrlConnectionRequest implements NetworkRequest {
return sb;
}
}

View File

@@ -27,7 +27,7 @@ import okhttp3.Request;
import okhttp3.Response;
/**
* okhttp实现网络请求
* okhttp瀹炵幇缃戠粶璇锋眰
*/
public class OkHttpRequest implements NetworkRequest {
@@ -39,35 +39,35 @@ public class OkHttpRequest implements NetworkRequest {
public OkHttpRequest(final Context context) {
client = new OkHttpClient.Builder()
// 这里配置连接池是为了方便测试httpdns能力正式代码请不要配置
// 杩欓噷閰嶇疆杩炴帴姹狅紝鏄负浜嗘柟渚挎祴璇昲ttpdns鑳藉姏锛屾寮忎唬鐮佽涓嶈閰嶇疆
.connectionPool(new ConnectionPool(0, 10 * 1000, TimeUnit.MICROSECONDS))
.dns(new Dns() {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
HTTPDNSResult result;
/* 切换为新版标准api */
/* 鍒囨崲涓烘柊鐗堟爣鍑哸pi */
if (async) {
result = MyApp.getInstance().getService().getHttpDnsResultForHostAsync(hostname, type);
} else {
result = MyApp.getInstance().getService().getHttpDnsResultForHostSync(hostname, type);
}
Log.d(TAG, "httpdns 解析 " + hostname + " 结果为 " + result + " ttl is " + Util.getTtl(result));
Log.d(TAG, "httpdns 瑙f瀽 " + hostname + " 缁撴灉涓?" + result + " ttl is " + Util.getTtl(result));
List<InetAddress> inetAddresses = new ArrayList<>();
// 这里需要根据实际情况选择使用ipv6地址 还是 ipv4地址 下面示例的代码优先使用了ipv6地址
// 杩欓噷闇€瑕佹牴鎹疄闄呮儏鍐甸€夋嫨浣跨敤ipv6鍦板潃 杩樻槸 ipv4鍦板潃锛?涓嬮潰绀轰緥鐨勪唬鐮佷紭鍏堜娇鐢ㄤ簡ipv6鍦板潃
Log.d(TAG, "netType is: " + HttpDnsNetworkDetector.getInstance().getNetType(context));
if (result.getIpv6s() != null && result.getIpv6s().length > 0 && HttpDnsNetworkDetector.getInstance().getNetType(context) != NetType.v4) {
for (int i = 0; i < result.getIpv6s().length; i++) {
inetAddresses.addAll(Arrays.asList(InetAddress.getAllByName(result.getIpv6s()[i])));
}
Log.d(TAG, "使用ipv6地址" + inetAddresses);
Log.d(TAG, "浣跨敤ipv6鍦板潃" + inetAddresses);
} else if (result.getIps() != null && result.getIps().length > 0 && HttpDnsNetworkDetector.getInstance().getNetType(context) != NetType.v6) {
for (int i = 0; i < result.getIps().length; i++) {
inetAddresses.addAll(Arrays.asList(InetAddress.getAllByName(result.getIps()[i])));
}
Log.d(TAG, "使用ipv4地址" + inetAddresses);
Log.d(TAG, "浣跨敤ipv4鍦板潃" + inetAddresses);
}
if (inetAddresses.size() == 0) {
Log.d(TAG, "httpdns 未返回IPlocaldns");
Log.d(TAG, "httpdns 鏈繑鍥濱P锛岃蛋localdns");
return Dns.SYSTEM.lookup(hostname);
}
return inetAddresses;
@@ -84,14 +84,15 @@ public class OkHttpRequest implements NetworkRequest {
@Override
public String httpGet(String url) throws Exception {
Log.d(TAG, "使用okhttp 请求" + url + " 异步接口 " + async + " ip类型 " + type.name());
Log.d(TAG, "浣跨敤okhttp 璇锋眰" + url + " 寮傛鎺ュ彛 " + async + " ip绫诲瀷 " + type.name());
Response response = client.newCall(new Request.Builder().url(url).build()).execute();
int code = response.code();
String body = response.body().string();
Log.d(TAG, "使用okhttp 请求结果 code " + code + " body " + body);
Log.d(TAG, "浣跨敤okhttp 璇锋眰缁撴灉 code " + code + " body " + body);
if (code != HttpURLConnection.HTTP_OK) {
throw new Exception("请求失败 code " + code + " body " + body);
throw new Exception("璇锋眰澶辫触 code " + code + " body " + body);
}
return body;
}
}

View File

@@ -25,3 +25,4 @@ public class SpUtil {
void onGetSpEditor(SharedPreferences.Editor editor);
}
}

View File

@@ -26,14 +26,14 @@ public class ThreadUtil {
}
final int tmpValidCount = validCount;
Log.d(MyApp.TAG,
threadCount + "线程并发,执行" + executeTime + ", 总域名" + hostCount + "个,有效"
+ validCount + "");
threadCount + "绾跨▼骞跺彂锛屾墽琛? + executeTime + ", 鎬诲煙鍚? + hostCount + "涓紝鏈夋晥"
+ validCount + "涓?);
new Thread(new Runnable() {
@Override
public void run() {
final ArrayList<String> hosts = new ArrayList<>(hostCount);
for (int i = 0; i < hostCount - tmpValidCount; i++) {
hosts.add("test" + i + ".aliyun.com");
hosts.add("test" + i + ".Aliyun.com");
}
hosts.addAll(Arrays.asList(validHosts).subList(0, tmpValidCount));
@@ -125,3 +125,4 @@ public class ThreadUtil {
}).start();
}
}

View File

@@ -6,8 +6,8 @@ import java.lang.reflect.Field;
public class Util {
/**
* 获取ttl
* 此方法是用于测试自定义ttl是否生效
* 鑾峰彇ttl锛?
* 姝ゆ柟娉曟槸鐢ㄤ簬娴嬭瘯鑷畾涔塼tl鏄惁鐢熸晥
*/
public static int getTtl(HTTPDNSResult result) {
try {
@@ -22,3 +22,4 @@ public class Util {
return -1;
}
}

View File

@@ -31,7 +31,7 @@
android:id="@+id/bar_more"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="更多"
android:text="鏇村"
android:textSize="16sp"
android:gravity="center"
android:paddingLeft="10dp"

View File

@@ -1,31 +1,31 @@
<resources>
<string name="app_name">【阿里云HttpDns】Demo程序</string>
<string name="app_name">銆愰樋閲屼簯HttpDns銆慏emo绋嬪簭</string>
<string name="action_settings">Settings</string>
<string name="normal_parse">普通解析</string>
<string name="request_taobao">解析淘宝域名</string>
<string name="request_apple">解析apple域名</string>
<string name="request_douban">解析豆瓣域名</string>
<string name="https_parse">HTTPS开关</string>
<string name="timeout">设置超时</string>
<string name="set_expired">允许过期域名</string>
<string name="set_cache">开启持久化缓存</string>
<string name="set_degration_filter">降级策略</string>
<string name="set_pre_resolve">预解析</string>
<string name="normal_parse">鏅€氳В鏋?/string>
<string name="request_taobao">瑙f瀽娣樺疂鍩熷悕</string>
<string name="request_apple">瑙f瀽apple鍩熷悕</string>
<string name="request_douban">瑙f瀽璞嗙摚鍩熷悕</string>
<string name="https_parse">HTTPS寮€鍏?/string>
<string name="timeout">璁剧疆瓒呮椂</string>
<string name="set_expired">鍏佽杩囨湡鍩熷悕</string>
<string name="set_cache">寮€鍚寔涔呭寲缂撳瓨</string>
<string name="set_degration_filter">闄嶇骇绛栫暐</string>
<string name="set_pre_resolve">棰勮В鏋?/string>
<string name="set_region">region</string>
<string name="sync_request">同步解析</string>
<string name="multi_sync_request">同步解析并发</string>
<string name="multi_request">并发解析</string>
<string name="sync_request">鍚屾瑙f瀽</string>
<string name="multi_sync_request">鍚屾瑙f瀽骞跺彂</string>
<string name="multi_request">骞跺彂瑙f瀽</string>
<string name="main_about_us">关于我们</string>
<string name="main_helper">帮助中心</string>
<string name="main_clear_text">清除当前消息</string>
<string name="main_about_us">鍏充簬鎴戜滑</string>
<string name="main_helper">甯姪涓績</string>
<string name="main_clear_text">娓呴櫎褰撳墠娑堟伅</string>
<!--关于我们页面-->
<!--鍏充簬鎴戜滑椤甸潰-->
<string name="layout_aboutus_arr">All Rights Reserved.</string>
<string name="layout_aboutus_company">阿里云(软件)有限公司版权所有</string>
<string name="layout_aboutus_copyright">Copyright © 2009 - 2016 Aliyun.com</string>
<string name="layout_aboutus_company">闃块噷浜?杞欢)鏈夐檺鍏徃鐗堟潈鎵€鏈?/string>
<string name="layout_aboutus_copyright">Copyright 2009 - 2016 Aliyun.com</string>
<string name="layout_aboutus_app_version">1.1.3</string>
<!--帮助中心页面-->
<string name="layout_helpus_content">Q : 什么是用户体验Demo\nA : 用户体验Demo就是阿里云平台为您自动创建的、用来体验HttpDns服务和反馈建议用的一个小Demo让您体验便捷、高效的HttpDns服务。\n\nQ : 如何联系我们?\nA : App Demo相关问题请搜索钉钉群号11777313</string>
<!--甯姪涓績椤甸潰-->
<string name="layout_helpus_content">Q : 浠€涔堟槸鐢ㄦ埛浣撻獙Demo锛焅nA : 鐢ㄦ埛浣撻獙Demo灏辨槸闃块噷浜戝钩鍙颁负鎮ㄨ嚜鍔ㄥ垱寤虹殑銆佺敤鏉ヤ綋楠孒ttpDns鏈嶅姟鍜屽弽棣堝缓璁敤鐨勪竴涓皬Demo锛岃鎮ㄤ綋楠屼究鎹枫€侀珮鏁堢殑HttpDns鏈嶅姟銆俓n\nQ : 濡備綍鑱旂郴鎴戜滑锛焅nA : App Demo鐩稿叧闂锛岃鎼滅储閽夐拤缇ゅ彿锛?1777313</string>
</resources>

View File

@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<Alibaba-anchors>
<certificates src="system" />
</trust-anchors>
</Alibaba-anchors>
</base-config>
</network-security-config>
</network-security-config>