CDN加速

Android示例

更新时间:2020-04-27 14:42:52

针对HttpURLConnection接口。
try {
String url = “https://58.215.139.22/?ws_domain=center.httpdns.com&ws_ret_type=json&ws_cli_IP=218.85.152.99”;
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
connection.setRequestProperty(“Host”, “httpdns.wangsucloud.com”);
connection.setHostnameVerifier(new HostnameVerifier() {
/*

  • 关于这个接口的说明,官方有文档描述:
  • This is an extended verification option that implementers can provide.
  • It is to be used during a handshake if the URL’s hostname does not match the
  • peer’s identification hostname.
  • 使用HTTPDNS后URL里设置的hostname不是远程的主机名(如:httpdns.wangsucloud.com),与证书颁发的域不匹配,
  • Android HttpsURLConnection提供了回调接口让用户来处理这种定制化场景。
  • 在确认HTTPDNS返回的源站IP与Session携带的IP信息一致后,您可以在回调方法中将待验证域名替换为httpdns.wangsucloud.com进行验证。

*/
@Override
public boolean verify(String hostname, SSLSession session) {
return HttpsURLConnection.getDefaultHostnameVerifier().verify(“httpdns.wangsucloud.com”, session);
return false;
}
});
connection.connect();
} catch (Exception e) {
e.printStackTrace();
} finally {
}