53c5fd4658e1a7d9c2b26bbf49997eeb8703e0d4.svn-base 10.0 KB

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