日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Java 調(diào)用Restful API接口的幾種方式

 陳永正的圖書館 2017-09-12

摘要:最近有一個(gè)需求,為客戶提供一些Restful API 接口,QA使用postman進(jìn)行測(cè)試,但是postman的測(cè)試接口與java調(diào)用的相似但并不相同,于是想自己寫一個(gè)程序去測(cè)試Restful API接口,由于使用的是HTTPS,所以還要考慮到對(duì)于HTTPS的處理。由于我也是首次使用Java調(diào)用restful接口,所以還要研究一番,自然也是查閱了一些資料。

分析:這個(gè)問題與模塊之間的調(diào)用不同,比如我有兩個(gè)模塊front end 和back end,front end提供前臺(tái)展示,back end提供數(shù)據(jù)支持。之前使用過Hession去把back end提供的服務(wù)注冊(cè)成遠(yuǎn)程服務(wù),在front end端可以通過這種遠(yuǎn)程服務(wù)直接調(diào)到back end的接口。但這對(duì)于一個(gè)公司自己的一個(gè)項(xiàng)目耦合性比較高的情況下使用,沒有問題。但是如果給客戶注冊(cè)這種遠(yuǎn)程服務(wù),似乎不太好,耦合性太高。所以就考慮用一下方式進(jìn)行處理。


一、HttpClient


HttpClient大家也許比較熟悉但又比較陌生,熟悉是知道他可以遠(yuǎn)程調(diào)用比如請(qǐng)求一個(gè)URL,然后在response里獲取到返回狀態(tài)和返回信息,但是今天講的稍微復(fù)雜一點(diǎn),因?yàn)榻裉斓闹黝}是HTTPS,這個(gè)牽涉到證書或用戶認(rèn)證的問題。

確定使用HttpClient之后,查詢相關(guān)資料,發(fā)現(xiàn)HttpClient的新版本與老版本不同,隨然兼容老版本,但已經(jīng)不提倡老版本是使用方式,很多都已經(jīng)標(biāo)記為過時(shí)的方法或類。今天就分別使用老版本4.2和最新版本4.5.3來(lái)寫代碼。


老版本4.2


需要認(rèn)證



在準(zhǔn)備證書階段選擇的是使用證書認(rèn)證

  1. package com.darren.test.https.v42;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.security.KeyStore;  
  6.   
  7. import org.apache.http.conn.ssl.SSLSocketFactory;  
  8.   
  9. public class HTTPSCertifiedClient extends HTTPSClient {  
  10.   
  11.     public HTTPSCertifiedClient() {  
  12.   
  13.     }  
  14.   
  15.     @Override  
  16.     public void prepareCertificate() throws Exception {  
  17.         // 獲得密匙庫(kù)  
  18.         KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());  
  19.         FileInputStream instream = new FileInputStream(  
  20.                 new File("C:/Users/zhda6001/Downloads/software/xxx.keystore"));  
  21.         // FileInputStream instream = new FileInputStream(new File("C:/Users/zhda6001/Downloads/xxx.keystore"));  
  22.         // 密匙庫(kù)的密碼  
  23.         trustStore.load(instream, "password".toCharArray());  
  24.         // 注冊(cè)密匙庫(kù)  
  25.         this.socketFactory = new SSLSocketFactory(trustStore);  
  26.         // 不校驗(yàn)域名  
  27.         socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
  28.     }  
  29. }  


跳過認(rèn)證


在準(zhǔn)備證書階段選擇的是跳過認(rèn)證

  1. package com.darren.test.https.v42;  
  2.   
  3. import java.security.cert.CertificateException;  
  4. import java.security.cert.X509Certificate;  
  5.   
  6. import javax.net.ssl.SSLContext;  
  7. import javax.net.ssl.TrustManager;  
  8. import javax.net.ssl.X509TrustManager;  
  9.   
  10. import org.apache.http.conn.ssl.SSLSocketFactory;  
  11.   
  12. public class HTTPSTrustClient extends HTTPSClient {  
  13.   
  14.     public HTTPSTrustClient() {  
  15.   
  16.     }  
  17.   
  18.     @Override  
  19.     public void prepareCertificate() throws Exception {  
  20.         // 跳過證書驗(yàn)證  
  21.         SSLContext ctx = SSLContext.getInstance("TLS");  
  22.         X509TrustManager tm = new X509TrustManager() {  
  23.             @Override  
  24.             public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
  25.             }  
  26.   
  27.             @Override  
  28.             public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
  29.             }  
  30.   
  31.             @Override  
  32.             public X509Certificate[] getAcceptedIssuers() {  
  33.                 return null;  
  34.             }  
  35.         };  
  36.         // 設(shè)置成已信任的證書  
  37.         ctx.init(null, new TrustManager[] { tm }, null);  
  38.         // 穿件SSL socket 工廠,并且設(shè)置不檢查host名稱  
  39.         this.socketFactory = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
  40.     }  
  41. }  


總結(jié)


現(xiàn)在發(fā)現(xiàn)這兩個(gè)類都繼承了同一個(gè)類HTTPSClient,并且HTTPSClient繼承了DefaultHttpClient類,可以發(fā)現(xiàn),這里使用了模板方法模式。

  1. package com.darren.test.https.v42;  
  2.   
  3. import org.apache.http.conn.ClientConnectionManager;  
  4. import org.apache.http.conn.scheme.Scheme;  
  5. import org.apache.http.conn.scheme.SchemeRegistry;  
  6. import org.apache.http.conn.ssl.SSLSocketFactory;  
  7. import org.apache.http.impl.client.DefaultHttpClient;  
  8.   
  9. public abstract class HTTPSClient extends DefaultHttpClient {  
  10.   
  11.     protected SSLSocketFactory socketFactory;  
  12.   
  13.     /** 
  14.      * 初始化HTTPSClient 
  15.      *  
  16.      * @return 返回當(dāng)前實(shí)例 
  17.      * @throws Exception 
  18.      */  
  19.     public HTTPSClient init() throws Exception {  
  20.         this.prepareCertificate();  
  21.         this.regist();  
  22.   
  23.         return this;  
  24.     }  
  25.   
  26.     /** 
  27.      * 準(zhǔn)備證書驗(yàn)證 
  28.      *  
  29.      * @throws Exception 
  30.      */  
  31.     public abstract void prepareCertificate() throws Exception;  
  32.   
  33.     /** 
  34.      * 注冊(cè)協(xié)議和端口, 此方法也可以被子類重寫 
  35.      */  
  36.     protected void regist() {  
  37.         ClientConnectionManager ccm = this.getConnectionManager();  
  38.         SchemeRegistry sr = ccm.getSchemeRegistry();  
  39.         sr.register(new Scheme("https", 443, socketFactory));  
  40.     }  
  41. }  

下邊是工具類

  1. package com.darren.test.https.v42;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Set;  
  7.   
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.NameValuePair;  
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  12. import org.apache.http.client.methods.HttpGet;  
  13. import org.apache.http.client.methods.HttpPost;  
  14. import org.apache.http.client.methods.HttpRequestBase;  
  15. import org.apache.http.message.BasicNameValuePair;  
  16. import org.apache.http.util.EntityUtils;  
  17.   
  18. public class HTTPSClientUtil {  
  19.     private static final String DEFAULT_CHARSET = "UTF-8";  
  20.   
  21.     public static String doPost(HTTPSClient httpsClient, String url, Map<String, String> paramHeader,  
  22.             Map<String, String> paramBody) throws Exception {  
  23.         return doPost(httpsClient, url, paramHeader, paramBody, DEFAULT_CHARSET);  
  24.     }  
  25.   
  26.     public static String doPost(HTTPSClient httpsClient, String url, Map<String, String> paramHeader,  
  27.             Map<String, String> paramBody, String charset) throws Exception {  
  28.   
  29.         String result = null;  
  30.         HttpPost httpPost = new HttpPost(url);  
  31.         setHeader(httpPost, paramHeader);  
  32.         setBody(httpPost, paramBody, charset);  
  33.   
  34.         HttpResponse response = httpsClient.execute(httpPost);  
  35.         if (response != null) {  
  36.             HttpEntity resEntity = response.getEntity();  
  37.             if (resEntity != null) {  
  38.                 result = EntityUtils.toString(resEntity, charset);  
  39.             }  
  40.         }  
  41.   
  42.         return result;  
  43.     }  
  44.       
  45.     public static String doGet(HTTPSClient httpsClient, String url, Map<String, String> paramHeader,  
  46.             Map<String, String> paramBody) throws Exception {  
  47.         return doGet(httpsClient, url, paramHeader, paramBody, DEFAULT_CHARSET);  
  48.     }  
  49.   
  50.     public static String doGet(HTTPSClient httpsClient, String url, Map<String, String> paramHeader,  
  51.             Map<String, String> paramBody, String charset) throws Exception {  
  52.   
  53.         String result = null;  
  54.         HttpGet httpGet = new HttpGet(url);  
  55.         setHeader(httpGet, paramHeader);  
  56.   
  57.         HttpResponse response = httpsClient.execute(httpGet);  
  58.         if (response != null) {  
  59.             HttpEntity resEntity = response.getEntity();  
  60.             if (resEntity != null) {  
  61.                 result = EntityUtils.toString(resEntity, charset);  
  62.             }  
  63.         }  
  64.   
  65.         return result;  
  66.     }  
  67.   
  68.     private static void setHeader(HttpRequestBase request, Map<String, String> paramHeader) {  
  69.         // 設(shè)置Header  
  70.         if (paramHeader != null) {  
  71.             Set<String> keySet = paramHeader.keySet();  
  72.             for (String key : keySet) {  
  73.                 request.addHeader(key, paramHeader.get(key));  
  74.             }  
  75.         }  
  76.     }  
  77.   
  78.     private static void setBody(HttpPost httpPost, Map<String, String> paramBody, String charset) throws Exception {  
  79.         // 設(shè)置參數(shù)  
  80.         if (paramBody != null) {  
  81.             List<NameValuePair> list = new ArrayList<NameValuePair>();  
  82.             Set<String> keySet = paramBody.keySet();  
  83.             for (String key : keySet) {  
  84.                 list.add(new BasicNameValuePair(key, paramBody.get(key)));  
  85.             }  
  86.   
  87.             if (list.size() > 0) {  
  88.                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);  
  89.                 httpPost.setEntity(entity);  
  90.             }  
  91.         }  
  92.     }  
  93. }  

然后是測(cè)試類:

  1. package com.darren.test.https.v42;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. public class HTTPSClientTest {  
  7.   
  8.     public static void main(String[] args) throws Exception {  
  9.         HTTPSClient httpsClient = null;  
  10.   
  11.         httpsClient = new HTTPSTrustClient().init();  
  12.         //httpsClient = new HTTPSCertifiedClient().init();  
  13.   
  14.         String url = "https://1.2.6.2:8011/xxx/api/getToken";  
  15.         //String url = "https://1.2.6.2:8011/xxx/api/getHealth";  
  16.   
  17.         Map<String, String> paramHeader = new HashMap<>();  
  18.         //paramHeader.put("Content-Type", "application/json");  
  19.         paramHeader.put("Accept", "application/xml");  
  20.         Map<String, String> paramBody = new HashMap<>();  
  21.         paramBody.put("client_id", "ankur.tandon.ap@xxx.com");  
  22.         paramBody.put("client_secret", "P@ssword_1");  
  23.         String result = HTTPSClientUtil.doPost(httpsClient, url, paramHeader, paramBody);  
  24.           
  25.         //String result = HTTPSClientUtil.doGet(httpsClient, url, null, null);  
  26.           
  27.         System.out.println(result);  
  28.     }  
  29.   
  30. }  

返回信息:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <token>jkf8RL0sw+Skkflj8RbKI5hP1bEQK8PrCuTZPpBINqMYKRMxY1kWCjmCfT191Zpp88VV1aGHW8oYNWjEYS0axpLuGAX89ejCoWNbikCc1UvfyesXHLktcJqyUFiVjevhrEQxJPHncLQYWP+Xse5oD9X8vKFKk7InNTMRzQK7YBTZ/e3U7gswM/5cvAHFl6o9rEq9cWPXavZNohyvnXsohSzDo+BXAtXxa1xpEDLy/8h/UaP4n4dlZDJJ3B8t1Xh+CRRIoMOPxf7c5wKhHtOkEOeXW+xoPQKKSx5CKWwJpPuGIIFWF/PaqWg+JUOsVT7QGdPv8PMWJ9DwEwjTdxguDg==</token>  



新版本4.5.3


需要認(rèn)證


  1. package com.darren.test.https.v45;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.security.KeyStore;  
  6.   
  7. import javax.net.ssl.SSLContext;  
  8.   
  9. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;  
  10. import org.apache.http.conn.ssl.TrustSelfSignedStrategy;  
  11. import org.apache.http.ssl.SSLContexts;  
  12.   
  13. public class HTTPSCertifiedClient extends HTTPSClient {  
  14.   
  15.     public HTTPSCertifiedClient() {  
  16.   
  17.     }  
  18.   
  19.     @Override  
  20.     public void prepareCertificate() throws Exception {  
  21.         // 獲得密匙庫(kù)  
  22.         KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());  
  23.         FileInputStream instream = new FileInputStream(  
  24.                 new File("C:/Users/zhda6001/Downloads/software/xxx.keystore"));  
  25.         // FileInputStream instream = new FileInputStream(new File("C:/Users/zhda6001/Downloads/xxx.keystore"));  
  26.         try {  
  27.             // 密匙庫(kù)的密碼  
  28.             trustStore.load(instream, "password".toCharArray());  
  29.         } finally {  
  30.             instream.close();  
  31.         }  
  32.   
  33.         SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, TrustSelfSignedStrategy.INSTANCE)  
  34.                 .build();  
  35.         this.connectionSocketFactory = new SSLConnectionSocketFactory(sslcontext);  
  36.     }  
  37.   
  38. }  


跳過認(rèn)證


  1. package com.darren.test.https.v45;  
  2.   
  3. import java.security.cert.CertificateException;  
  4. import java.security.cert.X509Certificate;  
  5.   
  6. import javax.net.ssl.SSLContext;  
  7. import javax.net.ssl.TrustManager;  
  8. import javax.net.ssl.X509TrustManager;  
  9.   
  10. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;  
  11.   
  12. public class HTTPSTrustClient extends HTTPSClient {  
  13.   
  14.     public HTTPSTrustClient() {  
  15.   
  16.     }  
  17.   
  18.     @Override  
  19.     public void prepareCertificate() throws Exception {  
  20.         // 跳過證書驗(yàn)證  
  21.         SSLContext ctx = SSLContext.getInstance("TLS");  
  22.         X509TrustManager tm = new X509TrustManager() {  
  23.             @Override  
  24.             public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
  25.             }  
  26.   
  27.             @Override  
  28.             public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
  29.             }  
  30.   
  31.             @Override  
  32.             public X509Certificate[] getAcceptedIssuers() {  
  33.                 return null;  
  34.             }  
  35.         };  
  36.         // 設(shè)置成已信任的證書  
  37.         ctx.init(null, new TrustManager[] { tm }, null);  
  38.         this.connectionSocketFactory = new SSLConnectionSocketFactory(ctx);  
  39.     }  
  40. }  


總結(jié)


  1. package com.darren.test.https.v45;  
  2.   
  3. import org.apache.http.config.Registry;  
  4. import org.apache.http.config.RegistryBuilder;  
  5. import org.apache.http.conn.socket.ConnectionSocketFactory;  
  6. import org.apache.http.conn.socket.PlainConnectionSocketFactory;  
  7. import org.apache.http.impl.client.CloseableHttpClient;  
  8. import org.apache.http.impl.client.HttpClientBuilder;  
  9. import org.apache.http.impl.client.HttpClients;  
  10. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;  
  11.   
  12. public abstract class HTTPSClient extends HttpClientBuilder {  
  13.     private CloseableHttpClient client;  
  14.     protected ConnectionSocketFactory connectionSocketFactory;  
  15.   
  16.     /** 
  17.      * 初始化HTTPSClient 
  18.      *  
  19.      * @return 返回當(dāng)前實(shí)例 
  20.      * @throws Exception 
  21.      */  
  22.     public CloseableHttpClient init() throws Exception {  
  23.         this.prepareCertificate();  
  24.         this.regist();  
  25.   
  26.         return this.client;  
  27.     }  
  28.   
  29.     /** 
  30.      * 準(zhǔn)備證書驗(yàn)證 
  31.      *  
  32.      * @throws Exception 
  33.      */  
  34.     public abstract void prepareCertificate() throws Exception;  
  35.   
  36.     /** 
  37.      * 注冊(cè)協(xié)議和端口, 此方法也可以被子類重寫 
  38.      */  
  39.     protected void regist() {  
  40.         // 設(shè)置協(xié)議http和https對(duì)應(yīng)的處理socket鏈接工廠的對(duì)象  
  41.         Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()  
  42.                 .register("http", PlainConnectionSocketFactory.INSTANCE)  
  43.                 .register("https", this.connectionSocketFactory)  
  44.                 .build();  
  45.         PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);  
  46.         HttpClients.custom().setConnectionManager(connManager);  
  47.   
  48.         // 創(chuàng)建自定義的httpclient對(duì)象  
  49.         this.client = HttpClients.custom().setConnectionManager(connManager).build();  
  50.         // CloseableHttpClient client = HttpClients.createDefault();  
  51.     }  
  52. }  


工具類:

  1. package com.darren.test.https.v45;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Set;  
  7.   
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.NameValuePair;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  13. import org.apache.http.client.methods.HttpGet;  
  14. import org.apache.http.client.methods.HttpPost;  
  15. import org.apache.http.client.methods.HttpRequestBase;  
  16. import org.apache.http.message.BasicNameValuePair;  
  17. import org.apache.http.util.EntityUtils;  
  18.   
  19. public class HTTPSClientUtil {  
  20.     private static final String DEFAULT_CHARSET = "UTF-8";  
  21.   
  22.     public static String doPost(HttpClient httpClient, String url, Map<String, String> paramHeader,  
  23.             Map<String, String> paramBody) throws Exception {  
  24.         return doPost(httpClient, url, paramHeader, paramBody, DEFAULT_CHARSET);  
  25.     }  
  26.   
  27.     public static String doPost(HttpClient httpClient, String url, Map<String, String> paramHeader,  
  28.             Map<String, String> paramBody, String charset) throws Exception {  
  29.   
  30.         String result = null;  
  31.         HttpPost httpPost = new HttpPost(url);  
  32.         setHeader(httpPost, paramHeader);  
  33.         setBody(httpPost, paramBody, charset);  
  34.   
  35.         HttpResponse response = httpClient.execute(httpPost);  
  36.         if (response != null) {  
  37.             HttpEntity resEntity = response.getEntity();  
  38.             if (resEntity != null) {  
  39.                 result = EntityUtils.toString(resEntity, charset);  
  40.             }  
  41.         }  
  42.   
  43.         return result;  
  44.     }  
  45.       
  46.     public static String doGet(HttpClient httpClient, String url, Map<String, String> paramHeader,  
  47.             Map<String, String> paramBody) throws Exception {  
  48.         return doGet(httpClient, url, paramHeader, paramBody, DEFAULT_CHARSET);  
  49.     }  
  50.   
  51.     public static String doGet(HttpClient httpClient, String url, Map<String, String> paramHeader,  
  52.             Map<String, String> paramBody, String charset) throws Exception {  
  53.   
  54.         String result = null;  
  55.         HttpGet httpGet = new HttpGet(url);  
  56.         setHeader(httpGet, paramHeader);  
  57.   
  58.         HttpResponse response = httpClient.execute(httpGet);  
  59.         if (response != null) {  
  60.             HttpEntity resEntity = response.getEntity();  
  61.             if (resEntity != null) {  
  62.                 result = EntityUtils.toString(resEntity, charset);  
  63.             }  
  64.         }  
  65.   
  66.         return result;  
  67.     }  
  68.   
  69.     private static void setHeader(HttpRequestBase request, Map<String, String> paramHeader) {  
  70.         // 設(shè)置Header  
  71.         if (paramHeader != null) {  
  72.             Set<String> keySet = paramHeader.keySet();  
  73.             for (String key : keySet) {  
  74.                 request.addHeader(key, paramHeader.get(key));  
  75.             }  
  76.         }  
  77.     }  
  78.   
  79.     private static void setBody(HttpPost httpPost, Map<String, String> paramBody, String charset) throws Exception {  
  80.         // 設(shè)置參數(shù)  
  81.         if (paramBody != null) {  
  82.             List<NameValuePair> list = new ArrayList<NameValuePair>();  
  83.             Set<String> keySet = paramBody.keySet();  
  84.             for (String key : keySet) {  
  85.                 list.add(new BasicNameValuePair(key, paramBody.get(key)));  
  86.             }  
  87.   
  88.             if (list.size() > 0) {  
  89.                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);  
  90.                 httpPost.setEntity(entity);  
  91.             }  
  92.         }  
  93.     }  
  94. }  


測(cè)試類:

  1. package com.darren.test.https.v45;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import org.apache.http.client.HttpClient;  
  7.   
  8. public class HTTPSClientTest {  
  9.   
  10.     public static void main(String[] args) throws Exception {  
  11.         HttpClient httpClient = null;  
  12.   
  13.         //httpClient = new HTTPSTrustClient().init();  
  14.         httpClient = new HTTPSCertifiedClient().init();  
  15.   
  16.         String url = "https://1.2.6.2:8011/xxx/api/getToken";  
  17.         //String url = "https://1.2.6.2:8011/xxx/api/getHealth";  
  18.   
  19.         Map<String, String> paramHeader = new HashMap<>();  
  20.         paramHeader.put("Accept", "application/xml");  
  21.         Map<String, String> paramBody = new HashMap<>();  
  22.         paramBody.put("client_id", "ankur.tandon.ap@xxx.com");  
  23.         paramBody.put("client_secret", "P@ssword_1");  
  24.         String result = HTTPSClientUtil.doPost(httpClient, url, paramHeader, paramBody);  
  25.           
  26.         //String result = HTTPSClientUtil.doGet(httpsClient, url, null, null);  
  27.           
  28.         System.out.println(result);  
  29.     }  
  30.   
  31. }  

結(jié)果:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <token>RxitF9//7NxwXJS2cjIjYhLtvzUNvMZxxEQtGN0u07sC9ysJeIbPqte3hCjULSkoXPEUYGUVeyI9jv7/WikLrzxYKc3OSpaTSM0kCbCKphu0TB2Cn/nfzv9fMLueOWFBdyz+N0sEiI9K+0Gp7920DFEncn17wUJVmC0u2jwvM5FAjQKmilwodXZ6a0Dq+D7dQDJwVcwxBvJ2ilhyIb3pr805Vppmi9atXrVAKO0ODa006wEJFOfcgyG5p70wpJ5rrBL85vfy9WCvkd1R7j6NVjhXgH2gNimHkjEJorMjdXW2gKiUsiWsELi/XPswao7/CTWNwTnctGK8PX2ZUB0ZfA==</token>  



二、HttpURLConnection


三、Spring的RestTemplate


其它方式以后補(bǔ)充


參考:

JAVA利用HttpClient進(jìn)行POST請(qǐng)求(HTTPS)

CloseableHttpClient加載證書來(lái)訪問https網(wǎng)站


在準(zhǔn)備證書階段選擇的是跳過認(rèn)證

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多