Android示例

更新时间:2023-11-09 17:08:30

针对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 {
}

本篇文档内容对您是否有帮助?
有帮助
我要反馈
提交成功!非常感谢您的反馈,我们会继续努力做到更好!