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();
}
This code is properly executing in JDK8 but if I try to execute it in JDK7, it is throwing javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated exception. I tried to google the issue. I found that we can write X509TrustManager and X509HostnameVerifier. I tried with these implementations also, but still didn't work. Please suggest me how can I execute it in JDK7. Again if I execute the code with X509TrustManager and X509HostnameVerifier, I am getting "java.net.SocketException: Connection reset"