javahttpsclient信任所有证书-创新互联

pack

创新互联成都网站建设按需定制制作,是成都网站营销推广公司,为加固提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站设计热线:13518219792age httpsclient;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import java.security.KeyStore;
import iF.*;

import javax.net.ssl.SSLContext;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;

import com.google.gson.Gson;

public class MyHttp {
   public static void main(String[] args) throws Exception{

//      KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
//      SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
//      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      SSLContext sslcontext = SSLContext.getInstance("SSLv3"); //建立证书实体
      javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
      javax.net.ssl.TrustManager tm = new miTM();
      trustAllCerts[0] = tm;
      sslcontext.init(null, trustAllCerts, null);
      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

       try{
           String url = "https://127.0.0.1:8787/";

           HttpPost httppost = new HttpPost(url);
           Gson gson = new Gson();
           EntityBuilder entitybuilder = EntityBuilder.create();
//           GetLiveListReq req = new GetLiveListReq();
//           req.setMsgType("GetLiveList");
//           req.setNum(1);
//           req.setStart(1);
//           req.setSessionId("1001");
           LoginReq req = new LoginReq();
           req.setMsgType("Login");
           req.setUserName("11");
           entitybuilder.setText(gson.toJson(req));
           httppost.setEntity(entitybuilder.build());

           System.out.println("executing request " + httppost.getURI());

           ResponseHandler responseHandler = new ResponseHandler(){
               public String handleResponse(final HttpResponse response) throws ClientProtocolException,IOException{
                   int status = response.getStatusLine().getStatusCode();
                   if (status >= 200 && status < 400){
                      System.out.println("Send success");
                       HttpEntity entity = response.getEntity();
                       return entity !=null ? EntityUtils.toString(entity) : null;
                   }else{
                       throw new ClientProtocolException("Unexpected response status: " + status);
                   }
               }
           };
           String responseBody = httpclient.execute(httppost,responseHandler);
           System.out.println("-------------------------------------------");
           System.out.println(responseBody);
           System.out.println("-------------------------------------------");
       }finally{
           httpclient.close();
       }
   }

   static class miTM implements javax.net.ssl.TrustManager,
   javax.net.ssl.X509TrustManager {
      public java.security.cert.X509Certificate[] getAcceptedIssuers() {
         return null;
      }

      public boolean isServerTrusted(
            java.security.cert.X509Certificate[] certs) {
         return true;
      }

      public boolean isClientTrusted(
            java.security.cert.X509Certificate[] certs) {
         return true;
      }

      public void checkServerTrusted(
            java.security.cert.X509Certificate[] certs, String authType)
                  throws java.security.cert.CertificateException {
         return;
      }

      public void checkClientTrusted(
            java.security.cert.X509Certificate[] certs, String authType)
                  throws java.security.cert.CertificateException {
         return;
      }
   }

}

javahttpsclient信
任所有证书
分享文章:javahttpsclient信任所有证书-创新互联
地址分享:http://bzwzjz.com/article/epdgc.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 响应式网站设计 成都网站制作 成都企业网站制作 企业网站建设 成都定制网站建设 手机网站制作设计 网站制作 高端网站设计 宜宾网站设计 成都网站设计 重庆电商网站建设 泸州网站建设 成都网站设计制作公司 专业网站设计 成都网站建设 营销网站建设 企业网站设计 成都网站设计公司 成都网站设计 自适应网站建设 教育网站设计方案 网站设计