网站建设资讯

NEWS

网站建设资讯

使用httpclient怎么实现https

使用httpclient怎么实现https,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都创新互联专业为企业提供三水网站建设、三水做网站、三水网站设计、三水网站制作等企业网站建设、网页设计与制作、三水企业网站模板建站服务,10年三水做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

package com.neusoft.sample.opscopereq;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateFactory;
import java.util.Base64;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManagerFactory;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class M {
    public static void main(String[] args) throws Exception {
        InputStream inputStream = M.class.getClassLoader().getResourceAsStream("sssssss.crt");
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null);
        String certificateAlias = Integer.toString(0);
        keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(inputStream));
        inputStream.close();
        SSLContext sslContext = SSLContext.getInstance("TLS");
        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());

        trustManagerFactory.init(keyStore);
        sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
           CloseableHttpClient client = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(new   HostnameVerifier() {
            
            public boolean verify(String hostname, SSLSession session) {
                // TODO Auto-generated method stub
                return true;
            }
        })   .build();
           
            HttpPost http = new HttpPost("www.url.com");
            
             String USERNAME = "user";

             String PASSWORD = "psw";
            String auth = USERNAME + ":" + PASSWORD;
            byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));
            String AUTH_HEADER_VAL = "Basic " + new String(encodedAuth);
            http.setHeader("Authorization", AUTH_HEADER_VAL);
            
            JSONObject paramObj = new JSONObject();
            paramObj.put("industryCode", "517");
            paramObj.put("keyWord", "批发");
            paramObj.put("pageNum", 1);
            HttpEntity entity=new StringEntity(paramObj.toJSONString(), "application/json;", "utf-8");
             
            http.setEntity(entity);

          CloseableHttpResponse res = client.execute(http);
          
          String aa = EntityUtils.toString( res.getEntity() );

         System.out.println(aa);
    }

}
 

    
        
       
            org.apache.httpcomponents
            httpcore
            4.4.10
       

       
       
            org.apache.httpcomponents
            httpclient
            4.5.6
       

        
        

看完上述内容,你们掌握使用httpclient怎么实现https的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站名称:使用httpclient怎么实现https
网页地址:http://cdweb.net/article/ihghes.html