java 调用 https 接口问题

shizy 发布于 2016/06/02 16:28
阅读 617
收藏 0

客户提供了个接口,  https://........

开始以为很简单, 后台模拟个发起个 http 请求就可以了, 今天试了试,发现完全想错了,

后来试着实现了下, 但还是不通

错误代码:javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

请教高手帮忙解决下.

加载中
0
不日小鸡
用https方式调用不久完了。。。
0
尚浩宇
尚浩宇
ClientConnectionManager ccm = httpclient.getConnectionManager();  
                        //register https protocol in httpclient's scheme registry  
            SchemeRegistry sr = ccm.getSchemeRegistry();  
            sr.register(new Scheme("https", 443, ssf));  
  
            HttpGet httpget = new HttpGet("");  
            HttpParams params = httpclient.getParams();  
  
            params.setParameter("param1", "paramValue1");  
  
            httpget.setParams(params);  
            System.out.println("REQUEST:" + httpget.getURI());



0
Shazi199
Shazi199
这种问题把错误信息拿去百度搜索一下就有了,还来问。。
0
红薯的小黄瓜
红薯的小黄瓜
 static Client client = null;
    static ClientConfig config = null;
    static {
        TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }


                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    }


                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    }
                }
        };


        SSLContext context = null;
        try {
            context = SSLContext.getInstance("TLS");
            context.init(null, trustAllCerts, new SecureRandom());
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());


        config = new DefaultClientConfig();
        config.getProperties().put(
                HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
                new HTTPSProperties(new HostnameVerifier() {
                    @Override
                    public boolean verify(String s, SSLSession sslSession) {
                        return true;
                    }
                }, context)
        );


    }


    public static void reg(String username, String password) throws Exception {
        client = Client.create(config);
        WebResource service = client.resource("https://www.baidu.com");
        JSONObject obj = new JSONObject();
        obj.put("username", username);
        obj.put("password", password);
        JSONObject response = service.type(MediaType.APPLICATION_JSON_TYPE).post(JSONObject.class, obj);
        System.out.println(response.toString());
        client.destroy();
    }
返回顶部
顶部