HttpUtil.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @Filename:HttpUtil.java
  3. * @Author:caiqf
  4. * @Date�?013-9-23
  5. */
  6. package com.happy.Unitil_nsh;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import java.io.BufferedReader;
  10. import java.io.DataOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.net.HttpURLConnection;
  14. import java.net.URL;
  15. import java.net.URLEncoder;
  16. import java.util.Iterator;
  17. import java.util.Map;
  18. import java.util.TreeMap;
  19. import java.util.zip.GZIPInputStream;
  20. import java.util.zip.ZipException;
  21. /**
  22. * @Class:HttpsUtil.java
  23. * @Description�?
  24. * @Author:caiqf
  25. * @Date�?013-9-23
  26. */
  27. @SuppressWarnings("all")
  28. public class HttpUtil {
  29. private static final Log log = LogFactory.getLog(HttpUtil.class);
  30. /**
  31. * HTTP协议GET请求方法
  32. */
  33. public static String httpMethodGet(String url, String gb) {
  34. if (null == gb || "".equals(gb)) {
  35. gb = "UTF-8";
  36. }
  37. StringBuffer sb = new StringBuffer();
  38. URL urls;
  39. HttpURLConnection uc = null;
  40. BufferedReader in = null;
  41. try {
  42. urls = new URL(url);
  43. uc = (HttpURLConnection) urls.openConnection();
  44. uc.setRequestMethod("GET");
  45. uc.connect();
  46. in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
  47. String readLine = "";
  48. while ((readLine = in.readLine()) != null) {
  49. sb.append(readLine);
  50. }
  51. if (in != null) {
  52. in.close();
  53. }
  54. if (uc != null) {
  55. uc.disconnect();
  56. }
  57. } catch (Exception e) {
  58. log.error(e.getMessage(), e);
  59. } finally {
  60. if (uc != null) {
  61. uc.disconnect();
  62. }
  63. }
  64. return sb.toString();
  65. }
  66. /**
  67. * HTTP协议POST请求方法
  68. */
  69. public static String httpMethodPost(String url, String params, String gb) {
  70. if (null == gb || "".equals(gb)) {
  71. gb = "UTF-8";
  72. }
  73. StringBuffer sb = new StringBuffer();
  74. URL urls;
  75. HttpURLConnection uc = null;
  76. BufferedReader in = null;
  77. try {
  78. urls = new URL(url);
  79. uc = (HttpURLConnection) urls.openConnection();
  80. uc.setRequestMethod("POST");
  81. uc.setDoOutput(true);
  82. uc.setDoInput(true);
  83. uc.setUseCaches(false);
  84. uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  85. uc.connect();
  86. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  87. out.write(params.getBytes(gb));
  88. out.flush();
  89. out.close();
  90. in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
  91. String readLine = "";
  92. while ((readLine = in.readLine()) != null) {
  93. sb.append(readLine);
  94. }
  95. if (in != null) {
  96. in.close();
  97. }
  98. if (uc != null) {
  99. uc.disconnect();
  100. }
  101. } catch (IOException e) {
  102. log.error(e.getMessage(), e);
  103. } finally {
  104. if (uc != null) {
  105. uc.disconnect();
  106. }
  107. }
  108. return sb.toString();
  109. }
  110. /**
  111. * HTTP协议POST请求方法
  112. */
  113. public static String httpMethodPostJson(String url, String params, String gb) {
  114. if (null == gb || "".equals(gb)) {
  115. gb = "UTF-8";
  116. }
  117. StringBuffer sb = new StringBuffer();
  118. URL urls;
  119. HttpURLConnection uc = null;
  120. BufferedReader in = null;
  121. try {
  122. urls = new URL(url);
  123. uc = (HttpURLConnection) urls.openConnection();
  124. uc.setRequestMethod("POST");
  125. uc.setDoOutput(true);
  126. uc.setDoInput(true);
  127. uc.setUseCaches(false);
  128. uc.setRequestProperty("Content-Type", "application/json");
  129. uc.connect();
  130. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  131. out.write(params.getBytes(gb));
  132. out.flush();
  133. out.close();
  134. in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
  135. String readLine = "";
  136. while ((readLine = in.readLine()) != null) {
  137. sb.append(readLine);
  138. }
  139. if (in != null) {
  140. in.close();
  141. }
  142. if (uc != null) {
  143. uc.disconnect();
  144. }
  145. } catch (IOException e) {
  146. log.error(e.getMessage(), e);
  147. } finally {
  148. if (uc != null) {
  149. uc.disconnect();
  150. }
  151. }
  152. return sb.toString();
  153. }
  154. /**
  155. * HTTP协议POST请求方法
  156. */
  157. public static String httpMethodPost(String url, TreeMap<String, String> paramsMap, String gb) {
  158. if (null == gb || "".equals(gb)) {
  159. gb = "UTF-8";
  160. }
  161. String params = null;
  162. if (null != paramsMap) {
  163. params = getParamStr(paramsMap);
  164. }
  165. System.out.println("====post请求参数= " + params);
  166. StringBuffer sb = new StringBuffer();
  167. URL urls;
  168. HttpURLConnection uc = null;
  169. BufferedReader in = null;
  170. try {
  171. urls = new URL(url);
  172. uc = (HttpURLConnection) urls.openConnection();
  173. uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  174. uc.setDoOutput(true);
  175. uc.setDoInput(true);
  176. uc.setRequestMethod("POST");
  177. uc.setUseCaches(false);
  178. uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  179. uc.connect();
  180. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  181. out.write(params.getBytes(gb));
  182. out.flush();
  183. out.close();
  184. in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
  185. String readLine = "";
  186. while ((readLine = in.readLine()) != null) {
  187. sb.append(readLine);
  188. }
  189. if (in != null) {
  190. in.close();
  191. }
  192. if (uc != null) {
  193. uc.disconnect();
  194. }
  195. } catch (IOException e) {
  196. log.error(e.getMessage(), e);
  197. } finally {
  198. if (uc != null) {
  199. uc.disconnect();
  200. }
  201. }
  202. return sb.toString();
  203. }
  204. public static String httpMethodPostGZIP(String url,
  205. TreeMap<String, String> paramsMap, String gb) {
  206. System.out.println("===url:" + url);
  207. if (null == gb || "".equals(gb)) {
  208. gb = "UTF-8";
  209. }
  210. String params = null;
  211. if (null != paramsMap) {
  212. params = getParamStr(paramsMap);
  213. }
  214. StringBuffer sb = new StringBuffer();
  215. URL urls;
  216. HttpURLConnection uc = null;
  217. BufferedReader in = null;
  218. try {
  219. urls = new URL(url);
  220. uc = (HttpURLConnection) urls.openConnection();
  221. uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  222. uc.setDoOutput(true);
  223. uc.setDoInput(true);
  224. uc.setRequestMethod("POST");
  225. uc.setUseCaches(false);
  226. uc.connect();
  227. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  228. out.write(params.getBytes(gb));
  229. out.flush();
  230. out.close();
  231. in = new BufferedReader(new InputStreamReader(new GZIPInputStream(uc.getInputStream()), gb));
  232. String readLine = "";
  233. while ((readLine = in.readLine()) != null) {
  234. sb.append(readLine).append("\n");
  235. }
  236. if (in != null) {
  237. in.close();
  238. }
  239. if (uc != null) {
  240. uc.disconnect();
  241. }
  242. } catch (ZipException e) {
  243. log.error("暂无数据");
  244. } catch (IOException e) {
  245. log.error(e.getMessage(), e);
  246. } finally {
  247. if (uc != null) {
  248. uc.disconnect();
  249. }
  250. }
  251. return sb.toString();
  252. }
  253. /**
  254. * HTTP协议POST请求添加参数的封装方�?
  255. */
  256. private static String getParamStr(TreeMap<String, String> paramsMap) {
  257. StringBuilder param = new StringBuilder();
  258. for (Iterator<Map.Entry<String, String>> it = paramsMap.entrySet().iterator(); it.hasNext(); ) {
  259. Map.Entry<String, String> e = it.next();
  260. param.append("&").append(URLEncoder.encode(e.getKey())).append("=").append(URLEncoder.encode(e.getValue()));
  261. }
  262. return param.toString().substring(1);
  263. }
  264. }