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. import javax.el.PropertyNotFoundException;
  14. import javax.net.ssl.HostnameVerifier;
  15. import javax.net.ssl.HttpsURLConnection;
  16. import javax.net.ssl.SSLSession;
  17. /**
  18. * https访问工具类
  19. * @author Cat
  20. *
  21. */
  22. public class HttpsClient {
  23. //读超时30s
  24. private final int timeout = 30000;
  25. private Map<String, String>   reqProperty = null;
  26. private String reqEncode = "UTF-8";
  27. private String respEncode = "UTF-8";
  28. public HttpsClient(){
  29. String trustStore = System.getProperty("javax.net.ssl.trustStore");
  30. //抑制证书域名与实际域名不匹配的警告
  31. HostnameVerifier hv = new HostnameVerifier() {
  32. public boolean verify(String urlHostName, SSLSession session) {
  33. System.out.println("Warning: URL Host: " + urlHostName
  34. + " vs. " + session.getPeerHost());
  35. return true;
  36. }
  37. };
  38. HttpsURLConnection.setDefaultHostnameVerifier(hv);
  39. //初始化请求参数
  40. reqProperty = new HashMap<String, String>();
  41. reqProperty.put("Content-Type", "text/xml");
  42. }
  43. //设置请求参数
  44. public void setRequestProperty(Map<String, String> reqProperty) {
  45. this.reqProperty.putAll(reqProperty);
  46. }
  47. public String doGet(String urlstr) throws IOException {
  48. URL url = new URL(urlstr);
  49. HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  50. fetchReqMap(connection);//设置请求属性
  51. connection.setReadTimeout(timeout);
  52. connection.setDoOutput(false); // true for POST, false for GET
  53. connection.setDoInput(true);
  54. connection.setRequestMethod("GET");
  55. connection.setUseCaches(false);
  56. String aLine = null;
  57. String ret = "";
  58. InputStream is = connection.getInputStream();
  59. BufferedReader aReader = new BufferedReader(new InputStreamReader(is, this.getRespEncode()));
  60. while ((aLine = aReader.readLine()) != null) {
  61. ret += aLine+ "\r\n";;
  62. }
  63. aReader.close();
  64. connection.disconnect();
  65. return ret;
  66. }
  67. public String doPost(String urlstr,byte data[]) throws IOException {
  68. URL url = new URL(urlstr);
  69. HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  70. fetchReqMap(connection);
  71. //      connection.setRequestProperty("SOAPAction","https://hms.wellcare.cn:8443/services/EnergyData");
  72. connection.setReadTimeout(timeout);
  73. connection.setDoOutput(true); // true for POST, false for GET
  74. connection.setDoInput(true);
  75. connection.setRequestMethod("POST");
  76. connection.setUseCaches(false);
  77. //写入post数据
  78. OutputStream out = connection.getOutputStream();
  79. out.write(data);
  80. //读出反馈结果
  81. String aLine = null;
  82. String ret = "";
  83. InputStream is = connection.getInputStream();
  84. BufferedReader aReader = new BufferedReader(new InputStreamReader(is, this.getRespEncode()));
  85. while ((aLine = aReader.readLine()) != null) {
  86. ret += aLine + "\r\n";
  87. }
  88. aReader.close();
  89. connection.disconnect();
  90. return ret;
  91. }
  92. /**
  93. * 直接将文件按二进制写入请求
  94. * @param urlstr
  95. * @param inputFile
  96. * @return
  97. * @throws IOException
  98. */
  99. public String doPost(String urlstr,File inputFile) throws IOException{
  100. FileInputStream fis = new FileInputStream(inputFile);
  101. byte[] data = new byte[(int) inputFile.length()];
  102. fis.read(data);
  103. fis.close();
  104. return doPost(urlstr, data);
  105. }
  106. /**
  107. * 字符串俺reqEncode编码方式二级制发送
  108. * @param urlstr
  109. * @param inputStr
  110. * @return
  111. * @throws IOException
  112. */
  113. public String doPost(String urlstr,String inputStr) throws IOException{
  114. byte[] data = inputStr.getBytes(this.getReqEncode());
  115. return doPost(urlstr, data);
  116. }
  117. private void fetchReqMap(HttpsURLConnection connection){
  118. Iterator<String> iterator = this.reqProperty.keySet().iterator();
  119. while(iterator.hasNext()){
  120. String key = iterator.next();
  121. connection.setRequestProperty(key, this.reqProperty.get(key));
  122. }
  123. }
  124. public void setRespEncode(String respEncode) {
  125. this.respEncode = respEncode;
  126. }
  127. public String getRespEncode() {
  128. return respEncode;
  129. }
  130. public void setReqEncode(String reqEncode) {
  131. this.reqEncode = reqEncode;
  132. }
  133. public String getReqEncode() {
  134. return reqEncode;
  135. }
  136. }
上一篇:Grunt教程——安装Grunt


下一篇:Azure HDInsight 上的 Spark 群集配合自定义的Python来分析网站日志