httpclent 访问https 忽略host检查 和一些 认证过程

本文详细介绍了如何使用Java的HttpClient进行HTTP客户端配置,并通过SSLContext实例化SSL上下文来实现HTTPS请求的发送,包括创建TrustManager信任管理器、初始化SSLContext、设置SSLSocketFactory、构建请求和执行POST操作等关键步骤。

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

http  4.3以前的 
HttpClient httpClient =		httpClient1;
    
    // 创建TrustManager
    X509TrustManager xtm = new X509TrustManager() {
      public void checkClientTrusted(X509Certificate[] chain,
          String authType) throws CertificateException {
      }
      
      public void checkServerTrusted(X509Certificate[] chain,
          String authType) throws CertificateException {
      }
      
      public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[] {};
      }
    };
    try {
      SSLContext ctx = SSLContext.getInstance("SSL");
      
      // 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
      ctx.init(null, new TrustManager[] { xtm }, null);
      
      SSLSocketFactory sf = new SSLSocketFactory(
          ctx,
          SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      Scheme sch = new Scheme("https", 443, sf);
      httpClient.getConnectionManager().getSchemeRegistry().register(sch);
      // 创建HttpPost
      HttpGet httpPost = new HttpGet(url); 
      httpPost.setHeader("content-type", contextType);
      // 执行POST请求
      HttpResponse response = httpClient.execute(httpPost); 
      // 获取响应实体
      HttpEntity entity = response.getEntity();
       bs = IOUtils.toByteArray(entity.getContent());
       if (null != entity) {
          EntityUtils.consume(entity); // Consume response content
        }
      return bs;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // 关闭连接,释放资源
//			httpClient.getConnectionManager().shutdown(); 
    }
    return bs;
  }

==========================================================================================================
4.3以后

SSLContext sslContext;
        try {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null,
                    new TrustStrategy() {
                        // 信任所有
                    public boolean isTrusted(X509Certificate[] chain,
                            String authType) throws CertificateException {
                        return true;
                    }

                }).build();


        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslContext,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf).build();

        HttpPost httpPost = new HttpPost(httpAddr2);
        //httpclient构建完成  构造请求参数
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("SENDER_CODE", spid));
        formparams.add(new BasicNameValuePair("TRX_CONTENT", base64Str));
        formparams.add(new BasicNameValuePair("SIGNATURE", md5Str));
        UrlEncodedFormEntity uefEntity;
        uefEntity = new UrlEncodedFormEntity(formparams);
        httpPost.setEntity(uefEntity);
        CloseableHttpResponse response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值