我的应用程序中的SSL验证有点问题。正如我在FourSquare的文档中找到的那样,他们建议验证如下:SSL Calidation,但问题是我使用HttpsURLConnection
并且我想使用该类而不是DefaultHttpClient
。因此,当我将getTolerantClient
中的客户端替换为HttpsURLConnection
并尝试执行此操作时,它会向我显示一些错误:
public HttpsURLConnection getTolerantClient() {
HttpsURLConnection client = new HttpsURLConnection();
SSLSocketFactory sslSocketFactory = (SSLSocketFactory) client.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
final X509HostnameVerifier delegate = sslSocketFactory.getHostnameVerifier();
if(!(delegate instanceof MyVerifier)) {
sslSocketFactory.setHostnameVerifier(new MyVerifier(delegate));
}
return client;
}
喜欢:
The method getConnectionManager() is undefined for the type HttpsURLConnection
&安培;
The method getHostnameVerifier() is undefined for the type SSLSocketFactory
所以任何想法我怎么能在我的情况下使用它? 提前谢谢!
答案 0 :(得分:1)
此代码示例可帮助您调整getTolerantClient
方法:
public URLConnection getTolerantClient(URL url) throws IOException {
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpsURLConnection)) {
/* not an https:// URL, nothing to do */
return conn;
}
HttpsURLConnection httpsconn = (HttpsURLConnection)conn;
final HostnameVerifier delegate = httpsconn.getHostnameVerifier();
if(!(delegate instanceof MyVerifier)) {
httpsconn.setHostnameVerifier(new MyVerifier(delegate));
}
return conn;
}
当然,MyVerifier
应该实现javax.net.ssl.HostnameVerifier
接口。