tencent cloud

移动解析 HTTPDNS

产品简介
产品介绍
应用场景
产品优势
产品限制
购买指南
计费说明
欠费说明
快速入门
开通移动解析 HTTPDNS
接入移动解析 HTTPDNS
操作指南
添加域名
解析量统计说明
解析监控
API 文档
配置信息说明
HTTP 请求方式查询
AES、DES 加密解密说明
API 接入实践教程
SDK 文档
SDK 快速接入
IOS SDK 文档
Android SDK 文档
访问管理及协作
访问管理概述
访问管理策略示例
常见问题
HTTPDNS 政策
隐私协议
数据处理和安全协议

HttpURLConnection 接入

PDF
聚焦模式
字号
最后更新时间: 2023-06-12 15:01:08
HTTPS 证书校验示例如下:
// 以域名为 www.qq.com,HTTPDNS 解析得到的 IP 为192.168.0.1为例
String url = "https://192.168.0.1/"; // 业务自己的请求连接
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
connection.setRequestProperty("Host", "www.qq.com");
connection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return HttpsURLConnection.getDefaultHostnameVerifier().verify("www.qq.com", session);
}
});
connection.setConnectTimeout(mTimeOut); // 设置连接超时
connection.setReadTimeout(mTimeOut); // 设置读流超时
connection.connect();
HTTPS + SNI 示例如下:
// 以域名为 www.qq.com,HttpDNS 解析得到的 IP 为192.168.0.1为例
String url = "https://192.168.0.1/"; // 用 HTTPDNS 解析得到的 IP 封装业务的请求 URL
HttpsURLConnection sniConn = null;
try {
sniConn = (HttpsURLConnection) new URL(url).openConnection();
// 设置HTTP请求头Host域
sniConn.setRequestProperty("Host", "www.qq.com");
sniConn.setConnectTimeout(3000);
sniConn.setReadTimeout(3000);
sniConn.setInstanceFollowRedirects(false);
// 定制SSLSocketFactory来带上请求域名 ***关键步骤
SniSSLSocketFactory sslSocketFactory = new SniSSLSocketFactory(sniConn);
sniConn.setSSLSocketFactory(sslSocketFactory);
// 验证主机名和服务器验证方案是否匹配
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return HttpsURLConnection.getDefaultHostnameVerifier().verify("原解析的域名", session);
}
};
sniConn.setHostnameVerifier(hostnameVerifier);
...
} catch (Exception e) {
Log.w(TAG, "Request failed", e);
} finally {
if (sniConn != null) {
sniConn.disconnect();
}
}

class SniSSLSocketFactory extends SSLSocketFactory {

private HttpsURLConnection mConn;

public SniSSLSocketFactory(HttpsURLConnection conn) {
mConn = conn;
}

@Override
public Socket createSocket() throws IOException {
return null;
}

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return null;
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return null;
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return null;
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return null;
}

@Override
public String[] getDefaultCipherSuites() {
return new String[0];
}

@Override
public String[] getSupportedCipherSuites() {
return new String[0];
}

@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
String realHost = mConn.getRequestProperty("Host");
if (realHost == null) {
realHost = host;
}
Log.i(TAG, "customized createSocket host is: " + realHost);
InetAddress address = socket.getInetAddress();
if (autoClose) {
socket.close();
}
SSLCertificateSocketFactory sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0);
SSLSocket ssl = (SSLSocket) sslSocketFactory.createSocket(address, port);
ssl.setEnabledProtocols(ssl.getSupportedProtocols());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Log.i(TAG, "Setting SNI hostname");
sslSocketFactory.setHostname(ssl, realHost);
} else {
Log.d(TAG, "No documented SNI support on Android < 4.2, trying with reflection");
try {
Method setHostnameMethod = ssl.getClass().getMethod("setHostname", String.class);
setHostnameMethod.invoke(ssl, realHost);
} catch (Exception e) {
Log.w(TAG, "SNI not useable", e);
}
}
// verify hostname and certificate
SSLSession session = ssl.getSession();
HostnameVerifier hostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
if (!hostnameVerifier.verify(realHost, session)) {
throw new SSLPeerUnverifiedException("Cannot verify hostname: " + realHost);
}
Log.i(TAG, "Established " + session.getProtocol() + " connection with " + session.getPeerHost() + " using " + session.getCipherSuite());
return ssl;
}
}


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈