d5931be76b58775da32b78640ffba9d5894b870a.svn-base 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package com.yihu.platform.utils;
  2. import java.io.*;
  3. import java.net.HttpURLConnection;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. /**
  11. * http工具类
  12. *
  13. * @author jcl
  14. * @company yihu.com
  15. * @since 2014-12-29
  16. */
  17. public class HttpUtil {
  18. private URL url;
  19. private HttpURLConnection conn;
  20. private Map<String, String> headers = new HashMap<String, String>();//头信息
  21. private static final String CHARSET = "utf-8";
  22. private static final String POST = "POST";
  23. private static final String GET = "GET";
  24. public HttpUtil(URL url) {
  25. this.url = url;
  26. }
  27. public HttpUtil(String url) throws MalformedURLException {
  28. this.url = new URL(url);
  29. }
  30. public String post(Map<String, String> param) {
  31. PrintWriter out = null;
  32. BufferedReader in = null;
  33. String result = "";
  34. try {
  35. // 打开和URL之间的连接
  36. conn = (HttpURLConnection) url.openConnection();
  37. conn.setDoOutput(true);
  38. conn.setDoInput(true);
  39. conn.setUseCaches(false);
  40. conn.setRequestMethod(POST);
  41. conn.setRequestProperty("accept", "*/*");
  42. conn.setRequestProperty("connection", "Keep-Alive");
  43. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  44. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + CHARSET);
  45. for (Entry<String, String> entry : headers.entrySet()) {
  46. conn.setRequestProperty(entry.getKey(), entry.getValue());
  47. }
  48. // 发送POST请求必须设置如下两行
  49. // 获取URLConnection对象对应的输出流
  50. out = new PrintWriter(conn.getOutputStream());
  51. // 发送请求参数
  52. StringBuilder paramStr = new StringBuilder();
  53. if (param != null) {
  54. for (Entry entry : param.entrySet()) {
  55. paramStr.append(entry.getKey()).append("=");
  56. if (StringUtil.isNotEmpty(entry.getValue())) paramStr.append(entry.getValue());
  57. paramStr.append("&");
  58. }
  59. if (paramStr.length() > 0) paramStr.deleteCharAt(paramStr.length() - 1);
  60. // System.out.println(paramStr);
  61. }
  62. out.print(paramStr.toString());
  63. // flush输出流的缓冲
  64. out.flush();
  65. // 定义BufferedReader输入流来读取URL的响应
  66. in = new BufferedReader(
  67. new InputStreamReader(conn.getInputStream(), CHARSET));
  68. String line;
  69. while ((line = in.readLine()) != null) {
  70. result += line;
  71. }
  72. } catch (Exception e) {
  73. System.out.println("发送POST请求出现异常!" + e);
  74. result = "发送POST请求出现异常!" + e;
  75. e.printStackTrace();
  76. }
  77. //使用finally块来关闭输出流、输入流
  78. finally {
  79. try {
  80. if (out != null) {
  81. out.close();
  82. }
  83. if (in != null) {
  84. in.close();
  85. }
  86. } catch (IOException ex) {
  87. ex.printStackTrace();
  88. }
  89. }
  90. return result;
  91. }
  92. public String get(Map<String, String> param) {
  93. String result = "";
  94. BufferedReader in = null;
  95. try {
  96. StringBuilder paramStr = new StringBuilder();
  97. if (param != null) {
  98. for (Entry entry : param.entrySet()) {
  99. paramStr.append(entry.getKey()).append("=");
  100. if (StringUtil.isNotEmpty(entry.getValue())) paramStr.append(entry.getValue());
  101. paramStr.append("&");
  102. }
  103. if (paramStr.length() > 0) paramStr.deleteCharAt(paramStr.length() - 1);
  104. // System.out.println(paramStr);
  105. }
  106. String urlNameString = url.toString();
  107. if (paramStr.length() > 0) {
  108. urlNameString += "?" + paramStr.toString();
  109. }
  110. System.out.println("请求串------->" + urlNameString);
  111. URL realUrl = new URL(urlNameString);
  112. // 打开和URL之间的连接
  113. conn = (HttpURLConnection) realUrl.openConnection();
  114. conn.setDoOutput(true);
  115. conn.setDoInput(true);
  116. conn.setUseCaches(false);
  117. conn.setRequestMethod(GET);
  118. for (Entry<String, String> entry : headers.entrySet()) {
  119. conn.setRequestProperty(entry.getKey(), entry.getValue());
  120. }
  121. conn.setRequestProperty("accept", "*/*");
  122. conn.setRequestProperty("connection", "Keep-Alive");
  123. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  124. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + CHARSET);
  125. // 建立实际的连接
  126. conn.connect();
  127. // 获取所有响应头字段
  128. Map<String, List<String>> map = conn.getHeaderFields();
  129. // 遍历所有的响应头字段
  130. // for (String key : map.keySet()) {
  131. // System.out.println(key + "--->" + map.get(key));
  132. // }
  133. // 定义 BufferedReader输入流来读取URL的响应
  134. in = new BufferedReader(new InputStreamReader(
  135. conn.getInputStream(), CHARSET));
  136. String line;
  137. while ((line = in.readLine()) != null) {
  138. result += line;
  139. }
  140. } catch (Exception e) {
  141. System.out.println("发送GET请求出现异常!" + e);
  142. e.printStackTrace();
  143. }
  144. // 使用finally块来关闭输入流
  145. finally {
  146. try {
  147. if (in != null) {
  148. in.close();
  149. }
  150. } catch (Exception e2) {
  151. e2.printStackTrace();
  152. }
  153. }
  154. return result;
  155. }
  156. public void setHeader(String key, String value) {
  157. headers.put(key, value);
  158. }
  159. public URL getUrl() {
  160. return url;
  161. }
  162. public void setUrl(URL url) {
  163. this.url = url;
  164. }
  165. public HttpURLConnection getConn() {
  166. return conn;
  167. }
  168. public void setConn(HttpURLConnection conn) {
  169. this.conn = conn;
  170. }
  171. public Map<String, String> getHeaders() {
  172. return headers;
  173. }
  174. public void setHeaders(Map<String, String> headers) {
  175. this.headers = headers;
  176. }
  177. public static void main(String[] args) throws Exception {
  178. HttpUtil httpUtil = new HttpUtil("http://ehr.yihu.com/wlyy/gc/accesstoken");
  179. Map map = new HashMap();
  180. map.put("appid", "915d0345-5b1d-11e6-8344-fa163e8aee56");
  181. map.put("appSecret", "915d0345-5b1d-11e6-8344-fa163e8aee52");
  182. String resp = httpUtil.post(map);
  183. System.out.println(resp);
  184. }
  185. }