HttpRequester.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package cn.com.lzt.common.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.nio.charset.Charset;
  10. import java.util.Map;
  11. import java.util.Vector;
  12. /**
  13. * 发送Http请求
  14. *
  15. * @author 刘鹏
  16. *
  17. */
  18. public class HttpRequester {
  19. private String defaultContentEncoding;
  20. private int timeOut;
  21. public HttpRequester() {
  22. defaultContentEncoding = Charset.defaultCharset().name();
  23. timeOut = 5;
  24. }
  25. /**
  26. * 发送GET请求
  27. *
  28. * @param urlString
  29. * URL地址
  30. * @return 响应对象
  31. * @throws IOException
  32. */
  33. public HttpRespons sendGet(String urlString) throws IOException {
  34. return send(urlString, "GET", null, null);
  35. }
  36. /**
  37. * 发送GET请求
  38. *
  39. * @param urlString
  40. * URL地址
  41. * @param params
  42. * 参数集合
  43. * @return 响应对象
  44. * @throws IOException
  45. */
  46. public HttpRespons sendGet(String urlString, Map<String, String> params)
  47. throws IOException {
  48. return send(urlString, "GET", params, null);
  49. }
  50. /**
  51. * 发送GET请求
  52. *
  53. * @param urlString
  54. * URL地址
  55. * @param params
  56. * 参数集合
  57. * @param propertys
  58. * 请求属性
  59. * @return 响应对象
  60. * @throws IOException
  61. */
  62. public HttpRespons sendGet(String urlString, Map<String, String> params,
  63. Map<String, String> propertys) throws IOException {
  64. return send(urlString, "GET", params, propertys);
  65. }
  66. /**
  67. * 发送POST请求
  68. *
  69. * @param urlString
  70. * URL地址
  71. * @return 响应对象
  72. * @throws IOException
  73. */
  74. public HttpRespons sendPost(String urlString) throws IOException {
  75. return send(urlString, "POST", null, null);
  76. }
  77. /**
  78. * 发送POST请求
  79. *
  80. * @param urlString
  81. * URL地址
  82. * @param params
  83. * 参数集合
  84. * @return 响应对象
  85. * @throws IOException
  86. */
  87. public HttpRespons sendPost(String urlString, Map<String, String> params)
  88. throws IOException {
  89. return send(urlString, "POST", params, null);
  90. }
  91. /**
  92. * 发送POST请求
  93. *
  94. * @param urlString
  95. * URL地址
  96. * @param params
  97. * 参数集合
  98. * @param propertys
  99. * 请求属性
  100. * @return 响应对象
  101. * @throws IOException
  102. */
  103. public HttpRespons sendPost(String urlString, Map<String, String> params,
  104. Map<String, String> propertys) throws IOException {
  105. return send(urlString, "POST", params, propertys);
  106. }
  107. public HttpRespons sendPost2SCF(String urlString, String parameters)
  108. throws IOException {
  109. HttpURLConnection urlConnection = null;
  110. URL url = new URL(urlString);
  111. urlConnection = (HttpURLConnection) url.openConnection();
  112. urlConnection.setConnectTimeout(timeOut);
  113. urlConnection.setRequestMethod("POST");
  114. urlConnection.setDoOutput(true);
  115. urlConnection.setDoInput(true);
  116. urlConnection.setUseCaches(false);
  117. urlConnection.setRequestProperty("Content-Type", "text/xml");
  118. urlConnection.getOutputStream().write(parameters.getBytes());
  119. urlConnection.getOutputStream().flush();
  120. urlConnection.getOutputStream().close();
  121. return makeContent(urlString, urlConnection);
  122. }
  123. public HttpRespons sendPost2Spring(String urlString, String parameters)
  124. throws IOException {
  125. HttpURLConnection urlConnection = null;
  126. URL url = new URL(urlString);
  127. urlConnection = (HttpURLConnection) url.openConnection();
  128. urlConnection.setConnectTimeout(timeOut);
  129. urlConnection.setRequestMethod("POST");
  130. urlConnection.setDoOutput(true);
  131. urlConnection.setDoInput(true);
  132. urlConnection.setUseCaches(false);
  133. urlConnection.setRequestProperty("Content-Type",
  134. "'application/json;charset=UTF-8");
  135. urlConnection.getOutputStream().write(parameters.getBytes());
  136. urlConnection.getOutputStream().flush();
  137. urlConnection.getOutputStream().close();
  138. return makeContent(urlString, urlConnection);
  139. }
  140. /**
  141. * 发送HTTP请求
  142. *
  143. * @param urlString
  144. * @return 响映对象
  145. * @throws IOException
  146. */
  147. private HttpRespons send(String urlString, String method,
  148. Map<String, String> parameters, Map<String, String> propertys)
  149. throws IOException {
  150. HttpURLConnection urlConnection = null;
  151. if (method.equalsIgnoreCase("GET") && parameters != null) {
  152. StringBuffer param = new StringBuffer();
  153. int i = 0;
  154. for (String key : parameters.keySet()) {
  155. if (i == 0) {
  156. param.append("?");
  157. } else {
  158. param.append("&");
  159. }
  160. param.append(key).append("=").append(parameters.get(key));
  161. i++;
  162. }
  163. urlString += param;
  164. }
  165. URL url = new URL(urlString);
  166. urlConnection = (HttpURLConnection) url.openConnection();
  167. urlConnection.setConnectTimeout(timeOut);
  168. urlConnection.setRequestMethod(method);
  169. urlConnection.setDoOutput(true);
  170. urlConnection.setDoInput(true);
  171. urlConnection.setUseCaches(false);
  172. urlConnection.setRequestProperty("Content-Type",
  173. "text/plain;charset=UTF-8");
  174. if (propertys != null) {
  175. for (String key : propertys.keySet()) {
  176. urlConnection.addRequestProperty(key, propertys.get(key));
  177. }
  178. }
  179. if (method.equalsIgnoreCase("POST") && parameters != null) {
  180. // urlConnection.setRequestProperty("Content-Type", "text/xml");
  181. StringBuffer param = new StringBuffer();
  182. for (String key : parameters.keySet()) {
  183. // param.append(parameters.get(key));
  184. param.append("&");
  185. param.append(key).append("=").append(parameters.get(key));
  186. }
  187. urlConnection.getOutputStream().write(param.toString().getBytes());
  188. urlConnection.getOutputStream().flush();
  189. urlConnection.getOutputStream().close();
  190. }
  191. return makeContent(urlString, urlConnection);
  192. }
  193. /**
  194. * 得到响应对象
  195. *
  196. * @param urlConnection
  197. * @return 响应对象
  198. * @throws IOException
  199. */
  200. private HttpRespons makeContent(String urlString,
  201. HttpURLConnection urlConnection) throws IOException {
  202. HttpRespons httpResponser = new HttpRespons();
  203. try {
  204. InputStream in = urlConnection.getInputStream();
  205. BufferedReader bufferedReader = new BufferedReader(
  206. new InputStreamReader(in));
  207. httpResponser.contentCollection = new Vector<String>();
  208. StringBuffer temp = new StringBuffer();
  209. String line = bufferedReader.readLine();
  210. while (line != null) {
  211. httpResponser.contentCollection.add(line);
  212. temp.append(line).append("\r\n");
  213. line = bufferedReader.readLine();
  214. }
  215. bufferedReader.close();
  216. String ecod = urlConnection.getContentEncoding();
  217. if (ecod == null) {
  218. ecod = defaultContentEncoding;
  219. }
  220. httpResponser.urlString = urlString;
  221. httpResponser.defaultPort = urlConnection.getURL().getDefaultPort();
  222. httpResponser.file = urlConnection.getURL().getFile();
  223. httpResponser.host = urlConnection.getURL().getHost();
  224. httpResponser.path = urlConnection.getURL().getPath();
  225. httpResponser.port = urlConnection.getURL().getPort();
  226. httpResponser.protocol = urlConnection.getURL().getProtocol();
  227. httpResponser.query = urlConnection.getURL().getQuery();
  228. httpResponser.ref = urlConnection.getURL().getRef();
  229. httpResponser.userInfo = urlConnection.getURL().getUserInfo();
  230. httpResponser.content = new String(temp.toString().getBytes(), ecod);
  231. httpResponser.contentEncoding = ecod;
  232. httpResponser.code = urlConnection.getResponseCode();
  233. httpResponser.message = urlConnection.getResponseMessage();
  234. httpResponser.contentType = urlConnection.getContentType();
  235. httpResponser.method = urlConnection.getRequestMethod();
  236. httpResponser.connectTimeout = urlConnection.getConnectTimeout();
  237. httpResponser.readTimeout = urlConnection.getReadTimeout();
  238. return httpResponser;
  239. } catch (IOException e) {
  240. System.out.print(e.getMessage());
  241. throw e;
  242. } finally {
  243. if (urlConnection != null) {
  244. urlConnection.disconnect();
  245. }
  246. }
  247. }
  248. /**
  249. * 发送HttpPost请求
  250. *
  251. * @param strURL
  252. * 服务地址
  253. * @param params
  254. * json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/>
  255. * @return 成功:返回json字符串<br/>
  256. */
  257. public static HttpRespons post(String strURL, String params) throws Exception{
  258. HttpRespons httpResponser = new HttpRespons();
  259. URL url = new URL(strURL);// 创建连接
  260. HttpURLConnection connection = (HttpURLConnection) url
  261. .openConnection();
  262. connection.setDoOutput(true);
  263. connection.setDoInput(true);
  264. connection.setUseCaches(false);
  265. connection.setInstanceFollowRedirects(true);
  266. connection.setRequestMethod("POST"); // 设置请求方式
  267. connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
  268. connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
  269. connection.connect();
  270. OutputStreamWriter out = new OutputStreamWriter(
  271. connection.getOutputStream(), "UTF-8"); // utf-8编码
  272. out.append(params);
  273. out.flush();
  274. out.close();
  275. // 读取响应
  276. InputStream is = connection.getInputStream();
  277. BufferedReader bufferedReader = new BufferedReader(
  278. new InputStreamReader(is));
  279. httpResponser.contentCollection = new Vector<String>();
  280. StringBuffer temp = new StringBuffer();
  281. String line = bufferedReader.readLine();
  282. while (line != null) {
  283. httpResponser.contentCollection.add(line);
  284. temp.append(line).append("\r\n");
  285. line = bufferedReader.readLine();
  286. }
  287. bufferedReader.close();
  288. httpResponser.urlString = strURL;
  289. httpResponser.defaultPort = connection.getURL().getDefaultPort();
  290. httpResponser.file = connection.getURL().getFile();
  291. httpResponser.host = connection.getURL().getHost();
  292. httpResponser.path = connection.getURL().getPath();
  293. httpResponser.port = connection.getURL().getPort();
  294. httpResponser.protocol = connection.getURL().getProtocol();
  295. httpResponser.query = connection.getURL().getQuery();
  296. httpResponser.ref = connection.getURL().getRef();
  297. httpResponser.userInfo = connection.getURL().getUserInfo();
  298. httpResponser.content = new String(temp.toString().getBytes(),
  299. "UTF-8");
  300. httpResponser.contentEncoding = "UTF-8";
  301. httpResponser.code = connection.getResponseCode();
  302. httpResponser.message = connection.getResponseMessage();
  303. httpResponser.contentType = connection.getContentType();
  304. httpResponser.method = connection.getRequestMethod();
  305. httpResponser.connectTimeout = connection.getConnectTimeout();
  306. httpResponser.readTimeout = connection.getReadTimeout();
  307. return httpResponser;
  308. }
  309. /**
  310. * 默认的响应字符集
  311. */
  312. public String getDefaultContentEncoding() {
  313. return defaultContentEncoding;
  314. }
  315. /**
  316. * 设置默认的响应字符集
  317. */
  318. public void setDefaultContentEncoding(String defaultContentEncoding) {
  319. this.defaultContentEncoding = defaultContentEncoding;
  320. }
  321. /**
  322. * 默认的连接超时秒数
  323. */
  324. public int getTimeOut() {
  325. return timeOut;
  326. }
  327. /**
  328. * 设置默认的超时秒数
  329. */
  330. public void setTimeOut(int timeOut) {
  331. this.timeOut = timeOut;
  332. }
  333. }