httpsClient实例

  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.io.OutputStream;  
  9. import java.net.URL;  
  10. import java.util.HashMap;  
  11. import java.util.Iterator;  
  12. import java.util.Map;  
  13.   
  14. import javax.el.PropertyNotFoundException;  
  15. import javax.net.ssl.HostnameVerifier;  
  16. import javax.net.ssl.HttpsURLConnection;  
  17. import javax.net.ssl.SSLSession;  
  18.   
  19. /** 
  20.  * https访问工具类 
  21.  * @author Cat 
  22.  * 
  23.  */  
  24. public class HttpsClient {  
  25.       
  26.     //读超时30s  
  27.     private final int timeout = 30000;  
  28.       
  29.     private Map<String, String>   reqProperty = null;  
  30.     private String reqEncode = "UTF-8";  
  31.     private String respEncode = "UTF-8";  
  32.       
  33.       
  34.     public HttpsClient(){  
  35.         String trustStore = System.getProperty("javax.net.ssl.trustStore");  
  36.           
  37.         //抑制证书域名与实际域名不匹配的警告  
  38.         HostnameVerifier hv = new HostnameVerifier() {  
  39.   
  40.             public boolean verify(String urlHostName, SSLSession session) {  
  41.                 System.out.println("Warning: URL Host: " + urlHostName  
  42.                 + " vs. " + session.getPeerHost());  
  43.                 return true;  
  44.             }  
  45.   
  46.         };  
  47.         HttpsURLConnection.setDefaultHostnameVerifier(hv);  
  48.           
  49.         //初始化请求参数  
  50.         reqProperty = new HashMap<String, String>();  
  51.         reqProperty.put("Content-Type", "text/xml");  
  52.           
  53.     }  
  54.       
  55.     //设置请求参数  
  56.     public void setRequestProperty(Map<String, String> reqProperty) {  
  57.         this.reqProperty.putAll(reqProperty);  
  58.     }  
  59.       
  60.     public String doGet(String urlstr) throws IOException {  
  61.         URL url = new URL(urlstr);  
  62.         HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();  
  63.         fetchReqMap(connection);//设置请求属性  
  64.         connection.setReadTimeout(timeout);  
  65.         connection.setDoOutput(false); // true for POST, false for GET  
  66.         connection.setDoInput(true);  
  67.         connection.setRequestMethod("GET");  
  68.         connection.setUseCaches(false);  
  69.           
  70.         String aLine = null;  
  71.         String ret = "";  
  72.         InputStream is = connection.getInputStream();  
  73.         BufferedReader aReader = new BufferedReader(new InputStreamReader(is, this.getRespEncode()));  
  74.           
  75.   
  76.         while ((aLine = aReader.readLine()) != null) {  
  77.             ret += aLine+ " ";;  
  78.         }  
  79.   
  80.   
  81.         aReader.close();  
  82.         connection.disconnect();  
  83.           
  84.         return ret;  
  85.     }  
  86.       
  87.     public String doPost(String urlstr,byte data[]) throws IOException {  
  88.         URL url = new URL(urlstr);  
  89.         HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();  
  90.         fetchReqMap(connection);  
  91. //      connection.setRequestProperty("SOAPAction","https://hms.wellcare.cn:8443/services/EnergyData");  
  92.           
  93.         connection.setReadTimeout(timeout);  
  94.         connection.setDoOutput(true); // true for POST, false for GET  
  95.         connection.setDoInput(true);  
  96.         connection.setRequestMethod("POST");  
  97.         connection.setUseCaches(false);  
  98.           
  99.         //写入post数据  
  100.         OutputStream out = connection.getOutputStream();  
  101.         out.write(data);  
  102.           
  103.         //读出反馈结果  
  104.         String aLine = null;  
  105.         String ret = "";  
  106.         InputStream is = connection.getInputStream();  
  107.         BufferedReader aReader = new BufferedReader(new InputStreamReader(is, this.getRespEncode()));  
  108.           
  109.   
  110.         while ((aLine = aReader.readLine()) != null) {  
  111.             ret += aLine + " ";  
  112.         }  
  113.   
  114.   
  115.         aReader.close();  
  116.         connection.disconnect();  
  117.           
  118.         return ret;  
  119.     }  
  120.       
  121.     /** 
  122.      * 直接将文件按二进制写入请求 
  123.      * @param urlstr 
  124.      * @param inputFile 
  125.      * @return 
  126.      * @throws IOException 
  127.      */  
  128.     public String doPost(String urlstr,File inputFile) throws IOException{  
  129.         FileInputStream fis = new FileInputStream(inputFile);  
  130.         byte[] data = new byte[(int) inputFile.length()];  
  131.         fis.read(data);  
  132.         fis.close();  
  133.         return doPost(urlstr, data);  
  134.           
  135.     }  
  136.       
  137.     /** 
  138.      * 字符串俺reqEncode编码方式二级制发送 
  139.      * @param urlstr 
  140.      * @param inputStr 
  141.      * @return 
  142.      * @throws IOException 
  143.      */  
  144.     public String doPost(String urlstr,String inputStr) throws IOException{  
  145.         byte[] data = inputStr.getBytes(this.getReqEncode());  
  146.         return doPost(urlstr, data);  
  147.     }  
  148.       
  149.       
  150.     private void fetchReqMap(HttpsURLConnection connection){  
  151.         Iterator<String> iterator = this.reqProperty.keySet().iterator();  
  152.         while(iterator.hasNext()){  
  153.             String key = iterator.next();  
  154.             connection.setRequestProperty(key, this.reqProperty.get(key));  
  155.         }  
  156.     }  
  157.   
  158.     public void setRespEncode(String respEncode) {  
  159.         this.respEncode = respEncode;  
  160.     }  
  161.   
  162.     public String getRespEncode() {  
  163.         return respEncode;  
  164.     }  
  165.   
  166.     public void setReqEncode(String reqEncode) {  
  167.         this.reqEncode = reqEncode;  
  168.     }  
  169.   
  170.     public String getReqEncode() {  
  171.         return reqEncode;  
  172.     }  
  173.   
  174.       
  175.   
  176. }
原文地址:https://www.cnblogs.com/Struts-pring/p/4305808.html