我正在尝试编写Java代码,我需要使用Apache HttpClient从RESTful Api中获取数据。我的Web服务器有一个自签名证书。我使用的代码如下:
public class WebClientDevWrapper {
public static HttpClient wrapClient(HttpClient base) {
try {
ClientConnectionManager ccm = base.getConnectionManager();
SSLSocketFactory sslsf = new SSLSocketFactory(new TrustSelfSignedStrategy());
Scheme https = new Scheme("https", 444, sslsf);
ccm.getSchemeRegistry().register(https);
return new DefaultHttpClient(ccm, base.getParams());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static void main(String[] args) throws ClientProtocolException, IOException{
HttpClient client ;
client = new DefaultHttpClient();
client = WebClientDevWrapper.wrapClient(client);
HttpGet request = new HttpGet("https://localhost/restapi");
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
}
运行此代码时,出现以下错误:“ javax.net.ssl.SSLException:的证书不包含CN或DNS subjectAlt”
请帮忙。
您需要在JAVA中导入SSL证书。使用下面的行
<JAVA_HOME>\bin\keytool -import -v -trustcacerts -alias [Your Alias name] -file [Your Certificate location] -keystore [Keystore location] -keypass [Your Password] -storepass [Your Password]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句