我必须连接到安全的https网址,然后按照以下步骤操作:
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。我创建了一个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。我执行了请求:
HttpResponse response = httpClient.execute(post);
回复显示访问被拒绝。
有人可以帮我理解我做错了吗?