尝试在Java中使用Apache HttpClient执行GET时出错
我正在尝试编写一个Java代码,需要使用ApacheHttpClient从RESTfulAPI获取数据。 我的web服务器具有自签名证书。我使用的代码如下:尝试在Java中使用Apache HttpClient执行GET时出错,java,rest,apache-httpclient-4.x,Java,Rest,Apache Httpclient 4.x,我正在尝试编写一个Java代码,需要使用ApacheHttpClient从RESTfulAPI获取数据。 我的web服务器具有自签名证书。我使用的代码如下: public class WebClientDevWrapper { public static HttpClient wrapClient(HttpClient base) { try { ClientConnectionManager ccm = base.getConnectionMa
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证书。使用下面的行
\bin\keytool-import-v-trustcacerts
-别名[您的别名]-文件[您的证书位置]
-密钥库[密钥库位置]-密钥传递[您的密码]
-storepass[您的密码]
CN代表“通用名”您确定用名称生成了您的证书吗?您是否使用keytool.exe导入了证书?它是否在您的jdk\jre\lib\security\cacerts路径下?然后,您是否使用-Djavax.net.ssl.trustStore JVM参数提供了证书路径?