通过 Apache Commons HttpClient 发送 HTTPS 请求

本文介绍了一种使用Java发送HTTPS POST请求的方法,包括设置代理、指定Content-Type、加载客户端证书及忽略服务器证书验证等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、通过 HTTPS 发送 POST 请求;

2、HTTPS 安全协议采用 TLSv1.2;

3、 使用代理(Proxy)进行 HTTPS 访问;

4、指定 Content-Type 为:application/x-www-form-urlencoded;

5、HTTPS  请求时加载客户端证书(Client Certificate);

6、忽略服务器端证书链(Server Certificate Chain)的校验(Validate)。

public static void main(String[] args) throws IOException, UnrecoverableKeyException, CertificateException, KeyStoreException, KeyManagementException {
        SSLConnectionSocketFactory socketFactory = getSocketFactory();
        
        // 创建 CloseableHttpClient 对象
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
        
        // 指定请求的 URL 并创建 HttpPost 对象
        HttpPost httppost = new HttpPost("https://xxxx/yyyy");
        
        // 设置请求通过的代理
        httppost.setConfig(RequestConfig.custom().setProxy(new HttpHost("host", 8080)).build());
        HttpEntity entity;
        
        // 设置请求的 ContentType 为 application/x-www-form-urlencoded
        httppost.addHeader(HttpHeaders.CONTENT_TYPE, Consts.HTTP_REQUEST_CONTENTTYPE_FORM);
        
        // 构建 POST 的内容
        List<BasicNameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("amount", "1.00"));
        entity = new UrlEncodedFormEntity(nvps, Consts.CHARSET_UTF8);
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            // 发送请求
            response = httpclient.execute(httppost);
            
            // 获取响应内容
            HttpEntity entity1 = response.getEntity();
            System.out.println(EntityUtils.toString(entity1));
        } finally {
            if (null != response) {
                response.close();
            }
            if (null != httpclient) {
                httpclient.close();
            }
        }
    }

    // 忽略服务器端证书链的认证
    private static TrustManager getTrustManagers() {
        return new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

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

    private static SSLConnectionSocketFactory getSocketFactory() throws IOException, KeyStoreException, CertificateException, UnrecoverableKeyException, KeyManagementException {
        SSLContext sslContext;
        try {
            // keyStore 用来存放客户端证书
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            FileInputStream instream = new FileInputStream(new File("d:\\test.p12"));
            try {
                keyStore.load(instream, "passwd".toCharArray());
            } finally {
                instream.close();
            }
            
            // 加载客户端证书,并设置HTTPS的安全协议为 TLSv1.2
            sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, "passwd".toCharArray()).useProtocol("TLSv1.2").build();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        try {
            sslContext.init(null, new TrustManager[]{getTrustManagers()}, new java.security.SecureRandom());
        } catch (KeyManagementException e) {
            return null;
        }
        return new SSLConnectionSocketFactory(sslContext);
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值