807471456c5e23a4d85ea86d9d2852e1ae176e2e.svn-base 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package com.chinacreator.process.util;
  2. import org.apache.log4j.Logger;
  3. import javax.net.ssl.*;
  4. import java.io.*;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.net.URLEncoder;
  8. import java.security.cert.CertificateException;
  9. import java.security.cert.X509Certificate;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Set;
  13. public class HttpBoYuanInvoke {
  14. protected static Logger log = Logger.getLogger(HttpBoYuanInvoke.class);
  15. private static class TrustAnyTrustManager implements X509TrustManager {
  16. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  17. }
  18. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  19. }
  20. public X509Certificate[] getAcceptedIssuers() {
  21. return new X509Certificate[]{};
  22. }
  23. }
  24. private static class TrustAnyHostnameVerifier implements HostnameVerifier {
  25. public boolean verify(String hostname, SSLSession session) {
  26. return true;
  27. }
  28. }
  29. /**
  30. * 处理https请求,超时时间默认为30秒
  31. * @param method
  32. * @param url
  33. * @param content
  34. * @param reqProperty
  35. * @return
  36. * @throws Exception
  37. */
  38. public static String sendhttpsReq(String method, String url, String content, Map reqProperty) throws Exception{
  39. return sendhttpsReq(method, url, content, reqProperty, 0);
  40. }
  41. /**
  42. * 处理https请求
  43. * @param method
  44. * @param url
  45. * @param content
  46. * @param reqProperty
  47. * @param timeout 超时时间, 如果小于或等于0,给默认值30秒
  48. * @return
  49. * @throws Exception
  50. */
  51. public static String sendhttpsReq(String method, String url, String content, Map reqProperty, int timeout) throws Exception{
  52. if(timeout <= 0){
  53. timeout = 30000; //如果超时时间小于或等于0,给默认值30秒
  54. }
  55. StringBuffer buffer = new StringBuffer();
  56. HttpsURLConnection connection = null;
  57. BufferedReader reader = null;
  58. OutputStream out = null;
  59. InputStream inputStream = null;
  60. InputStreamReader inputStreamReader = null;
  61. try {
  62. SSLContext sc = SSLContext.getInstance("TLSv1.2");
  63. sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
  64. connection = (HttpsURLConnection) new URL(url).openConnection();
  65. connection.setSSLSocketFactory(sc.getSocketFactory());
  66. connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
  67. connection.setRequestMethod(method);
  68. connection.setDoOutput(true);
  69. connection.setDoInput(true);
  70. //设置超时时间为30秒
  71. connection.setConnectTimeout(timeout);
  72. connection.setReadTimeout(timeout);
  73. if(reqProperty != null && reqProperty.size()>0){
  74. Set keySet = reqProperty.keySet();
  75. Iterator iterator = keySet.iterator();
  76. String key = "";
  77. String value = "";
  78. while(iterator.hasNext()){
  79. key = (String)iterator.next();
  80. value = (String)reqProperty.get(key);
  81. connection.setRequestProperty(key, value);
  82. }
  83. }
  84. connection.connect();
  85. out = connection.getOutputStream();
  86. out.write(content.getBytes("UTF-8"));
  87. out.flush();
  88. if (200 != connection.getResponseCode()) {
  89. }else{
  90. inputStream = connection.getInputStream();
  91. inputStreamReader = new InputStreamReader(inputStream, "utf-8");
  92. reader = new BufferedReader(inputStreamReader);
  93. String line = "";
  94. while ((line = reader.readLine()) != null) {
  95. buffer.append(line);
  96. }
  97. reader.close();
  98. }
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. log.error("调HTTP请求出现IO异常:"+e.getMessage());
  102. throw e;
  103. } catch (Exception e){
  104. e.printStackTrace();
  105. log.error("调HTTP请求出现异常:"+e.getMessage());
  106. throw e;
  107. } finally {
  108. if(inputStreamReader != null){
  109. try {
  110. inputStreamReader.close();
  111. } catch (IOException e) {
  112. inputStreamReader = null;
  113. log.error("释放inputStreamReader异常:"+e.getMessage());
  114. }
  115. }
  116. if(inputStream != null){
  117. try {
  118. inputStream.close();
  119. } catch (IOException e) {
  120. inputStream = null;
  121. log.error("释放inputStream异常:"+e.getMessage());
  122. }
  123. }
  124. if(out != null){
  125. try {
  126. out.close();
  127. } catch (IOException e) {
  128. out = null;
  129. log.error("释放out异常:"+e.getMessage());
  130. }
  131. }
  132. if(reader != null){
  133. try {
  134. reader.close();
  135. } catch (IOException e) {
  136. reader = null;
  137. log.error("释放reader异常:"+e.getMessage());
  138. }
  139. }
  140. if (connection != null) {
  141. connection.disconnect();
  142. }
  143. }
  144. return buffer.toString();
  145. }
  146. /**
  147. * 连接到服务器并获取数据,超时时间默认为30秒
  148. * GET方式发起请求
  149. * @param urlStr 请求URL
  150. * @param content 请求报文
  151. * @return
  152. * @throws Exception
  153. */
  154. public static String sendHttpByGet(String urlStr, String content, Map reqProperty) throws Exception {
  155. return sendHttpByGet(urlStr, content, reqProperty, 0);
  156. }
  157. /**
  158. * 连接到服务器并获取数据
  159. * GET方式发起请求
  160. * @param urlStr 请求URL
  161. * @param content 请求报文
  162. * @return
  163. * @throws Exception
  164. */
  165. public static String sendHttpByGet(String urlStr, String content, Map reqProperty, int timeout) throws Exception {
  166. if(timeout <= 0){
  167. timeout = 30000; //如果超时时间小于或等于0,给默认值30秒
  168. }
  169. URL url = null;
  170. HttpURLConnection connection = null;
  171. StringBuffer buffer = new StringBuffer();
  172. BufferedReader reader = null;
  173. OutputStream out = null;
  174. InputStream inputStream = null;
  175. InputStreamReader inputStreamReader = null;
  176. try {
  177. url = new URL(urlStr+"/"+URLEncoder.encode(content,"UTF-8"));
  178. connection = (HttpURLConnection) url.openConnection();
  179. if(reqProperty != null && reqProperty.size()>0){
  180. Set keySet = reqProperty.keySet();
  181. Iterator iterator = keySet.iterator();
  182. String key = "";
  183. String value = "";
  184. while(iterator.hasNext()){
  185. key = (String)iterator.next();
  186. value = (String)reqProperty.get(key);
  187. connection.setRequestProperty(key, value);
  188. }
  189. }
  190. //设置超时时间为30秒
  191. connection.setConnectTimeout(timeout);
  192. connection.setReadTimeout(timeout);
  193. connection.connect();
  194. if (200 != connection.getResponseCode()) {
  195. }else{
  196. inputStream = connection.getInputStream();
  197. inputStreamReader = new InputStreamReader(inputStream, "utf-8");
  198. reader = new BufferedReader(inputStreamReader);
  199. String line = "";
  200. while ((line = reader.readLine()) != null) {
  201. buffer.append(line);
  202. }
  203. reader.close();
  204. }
  205. } catch (IOException e) {
  206. e.printStackTrace();
  207. log.error("调HTTP请求出现IO异常:"+e.getMessage());
  208. throw e;
  209. } catch (Exception e){
  210. e.printStackTrace();
  211. log.error("调HTTP请求出现异常:"+e.getMessage());
  212. throw e;
  213. } finally {
  214. if(inputStreamReader != null){
  215. try {
  216. inputStreamReader.close();
  217. } catch (IOException e) {
  218. inputStreamReader = null;
  219. log.error("释放inputStreamReader异常:"+e.getMessage());
  220. }
  221. }
  222. if(inputStream != null){
  223. try {
  224. inputStream.close();
  225. } catch (IOException e) {
  226. inputStream = null;
  227. log.error("释放inputStream异常:"+e.getMessage());
  228. }
  229. }
  230. if(out != null){
  231. try {
  232. out.close();
  233. } catch (IOException e) {
  234. out = null;
  235. log.error("释放out异常:"+e.getMessage());
  236. }
  237. }
  238. if(reader != null){
  239. try {
  240. reader.close();
  241. } catch (IOException e) {
  242. reader = null;
  243. log.error("释放reader异常:"+e.getMessage());
  244. }
  245. }
  246. if (connection != null) {
  247. connection.disconnect();
  248. }
  249. }
  250. return buffer.toString();
  251. }
  252. /**
  253. * 连接到服务器并获取数据,超时时间默认为30秒
  254. * POST方式发起请求
  255. * @param method 请求方法,POST/PUT
  256. * @param urlStr 请求URL
  257. * @param content 请求报文
  258. * @param reqProperty 请求属性
  259. * @return
  260. * @throws Exception
  261. */
  262. public static String sendHttpByPost(String method, String urlStr, String content, Map reqProperty) throws Exception{
  263. return sendHttpByPost(method, urlStr, content, reqProperty, 0);
  264. }
  265. /**
  266. * 连接到服务器并获取数据
  267. * POST方式发起请求
  268. * @param method 请求方法,POST/PUT
  269. * @param urlStr 请求URL
  270. * @param content 请求报文
  271. * @param reqProperty 请求属性
  272. * @param timeout 超时时间,毫秒
  273. * @return
  274. * @throws Exception
  275. */
  276. public static String sendHttpByPost(String method, String urlStr, String content, Map reqProperty, int timeout) throws Exception{
  277. if(timeout <= 0){
  278. timeout = 30000; //如果超时时间小于或等于0,给默认值30秒
  279. }
  280. URL url = null;
  281. HttpURLConnection connection = null;
  282. StringBuffer buffer = new StringBuffer();
  283. BufferedReader reader = null;
  284. DataOutputStream out = null;
  285. InputStream inputStream = null;
  286. InputStreamReader inputStreamReader = null;
  287. try {
  288. url = new URL(urlStr);
  289. connection = (HttpURLConnection) url.openConnection();
  290. if(reqProperty != null && reqProperty.size()>0){
  291. Set keySet = reqProperty.keySet();
  292. Iterator iterator = keySet.iterator();
  293. String key = "";
  294. String value = "";
  295. while(iterator.hasNext()){
  296. key = (String)iterator.next();
  297. value = (String)reqProperty.get(key);
  298. connection.setRequestProperty(key, value);
  299. }
  300. }
  301. connection.setDoOutput(true);
  302. connection.setDoInput(true);
  303. connection.setRequestMethod(method);
  304. connection.setUseCaches(false);
  305. //设置超时时间为30秒
  306. connection.setConnectTimeout(timeout);
  307. connection.setReadTimeout(timeout);
  308. connection.connect();
  309. out = new DataOutputStream(connection.getOutputStream());
  310. out.write(content.getBytes("utf-8"));
  311. out.flush();
  312. out.close();
  313. inputStream = connection.getInputStream();
  314. inputStreamReader = new InputStreamReader(inputStream, "utf-8");
  315. reader = new BufferedReader(inputStreamReader);
  316. String line = "";
  317. while ((line = reader.readLine()) != null) {
  318. buffer.append(line);
  319. }
  320. reader.close();
  321. } catch (IOException e) {
  322. e.printStackTrace();
  323. log.error("调HTTP请求出现IO异常:"+e.getMessage());
  324. throw e;
  325. } catch (Exception e){
  326. e.printStackTrace();
  327. log.error("调HTTP请求出现异常:"+e.getMessage());
  328. throw e;
  329. } finally {
  330. if(inputStreamReader != null){
  331. try {
  332. inputStreamReader.close();
  333. } catch (IOException e) {
  334. inputStreamReader = null;
  335. log.error("释放inputStreamReader异常:"+e.getMessage());
  336. }
  337. }
  338. if(inputStream != null){
  339. try {
  340. inputStream.close();
  341. } catch (IOException e) {
  342. inputStream = null;
  343. log.error("释放inputStream异常:"+e.getMessage());
  344. }
  345. }
  346. if(out != null){
  347. try {
  348. out.close();
  349. } catch (IOException e) {
  350. out = null;
  351. log.error("释放out异常:"+e.getMessage());
  352. }
  353. }
  354. if(reader != null){
  355. try {
  356. reader.close();
  357. } catch (IOException e) {
  358. reader = null;
  359. log.error("释放reader异常:"+e.getMessage());
  360. }
  361. }
  362. if (connection != null) {
  363. connection.disconnect();
  364. }
  365. }
  366. return buffer.toString();
  367. }
  368. /**
  369. * 把map转换为url地址Params
  370. * @param map 存储参数的map
  371. * @return 返回完整的url地址
  372. */
  373. public static String mapToUrl(Map<String,String> map){
  374. StringBuilder param = new StringBuilder("");
  375. if (map == null) {
  376. return "";
  377. }
  378. for(Map.Entry<String,String> entry: map.entrySet()){
  379. String key = entry.getKey();
  380. String val = entry.getValue();
  381. param.append(key).append("=").append(val).append("&");
  382. }
  383. //删去最后的&
  384. if (param.toString().endsWith("&")) {
  385. param.deleteCharAt(param.length() - 1);
  386. }
  387. return param.toString();
  388. }
  389. }