0

我必须连接到安全的 https url,并按照以下步骤操作:

  1. 创建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);

响应显示访问被拒绝

有人可以帮助我理解我做错了什么吗?

4

0 回答 0