I have to connect to a secured https url and I followed these steps:
- Create the
HttpClient
:
client = new DefaultHttpClient();
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
registry.register(new Scheme("http", socketFactory, 80));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
2. I create a HttpPost
:
HttpPost post = new HttpPost("");
post.setHeader("Content-Type", "application/json");
post.setHeader("TOKEN", token);
post.setHeader("Accept", "*/*");
HttpEntity e = new StringEntity(json.toString());
post.setEntity(e);
3. I executed the request:
HttpResponse response = httpClient.execute(post);
The response shows that access is denied.
Can somebody help me to understand what I'm doing wrong?