HttpClient =new DefaultHttpClient();
HttpPost post = new HttpPost("https://myulr/token");
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>();
parametersBody.add(new BasicNameValuePair("client_id","my-client-id"));
parametersBody.add(new BasicNameValuePair("client_secret","my-secret-key"));
parametersBody.add(new BasicNameValuePair("scope","my-scope"));
parametersBody.add(new BasicNameValuePair("grant_type","client_credentials"));
try{
post.setEntity(new UrlEncodedFormEntity(parametersBody));
HttpResponse httpResponse = httpClient.execute(post);
int code = httpResponse.getStatusLine().getStatusCode();
System.out.println("Code::::: "+code);
String result=EntityUtils.toString(httpResponse.getEntity());
System.out.println("Result: "+result);
}catch(Exception e){
e.printStackTrace();
}
此代码在 JDK8 中正确执行,但如果我尝试在 JDK7 中执行它,它会抛出 javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated 异常。我试图用谷歌搜索这个问题。我发现我们可以写 X509TrustManager 和 X509HostnameVerifier。我也尝试了这些实现,但仍然没有工作。请建议我如何在JDK7中执行它。如果我再次使用 X509TrustManager 和 X509HostnameVerifier 执行代码,我会得到“java.net.SocketException:连接重置”