javahttpsclient信任所有證書-創(chuàng)新互聯(lián)

pack

創(chuàng)新互聯(lián)成都網(wǎng)站建設(shè)按需定制制作,是成都網(wǎng)站營(yíng)銷推廣公司,為加固提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計(jì)服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計(jì)、前端HTML5制作、后臺(tái)程序開發(fā)等。成都網(wǎng)站設(shè)計(jì)熱線: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"); //建立證書實(shí)體
      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<String> responseHandler = new ResponseHandler<String>(){
               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信任所有證書-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://www.muchs.cn/article12/epdgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、做網(wǎng)站手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站制作面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)