DefaultHttpClient,证书,Https:访问被拒绝

时间:2014-07-15 08:33:05

标签: java httpclient apache-httpclient-4.x httpsurlconnection sslsocketfactory

我必须连接到安全的https网址,然后按照以下步骤操作:

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

    回复显示访问被拒绝

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

0 个答案:

没有答案