c13c417c11c3b75d65c22a203c7df56380111bec.svn-base 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. e.printStackTrace();
  75. }
  76. //使用finally块来关闭输出流、输入流
  77. finally {
  78. try {
  79. if (out != null) {
  80. out.close();
  81. }
  82. if (in != null) {
  83. in.close();
  84. }
  85. } catch (IOException ex) {
  86. ex.printStackTrace();
  87. }
  88. }
  89. return result;
  90. }
  91. public String get(Map<String, String> param) {
  92. String result = "";
  93. BufferedReader in = null;
  94. try {
  95. StringBuilder paramStr = new StringBuilder();
  96. if (param != null) {
  97. for (Entry entry : param.entrySet()) {
  98. paramStr.append(entry.getKey()).append("=");
  99. if (StringUtil.isNotEmpty(entry.getValue())) paramStr.append(entry.getValue());
  100. paramStr.append("&");
  101. }
  102. if (paramStr.length() > 0) paramStr.deleteCharAt(paramStr.length() - 1);
  103. // System.out.println(paramStr);
  104. }
  105. String urlNameString = url.toString() + "?" + paramStr.toString();
  106. System.out.println("请求串------->" + urlNameString);
  107. URL realUrl = new URL(urlNameString);
  108. // 打开和URL之间的连接
  109. conn = (HttpURLConnection) realUrl.openConnection();
  110. conn.setDoOutput(true);
  111. conn.setDoInput(true);
  112. conn.setUseCaches(false);
  113. conn.setRequestMethod(GET);
  114. for (Entry<String, String> entry : headers.entrySet()) {
  115. conn.setRequestProperty(entry.getKey(), entry.getValue());
  116. }
  117. conn.setRequestProperty("accept", "*/*");
  118. conn.setRequestProperty("connection", "Keep-Alive");
  119. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  120. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="+CHARSET);
  121. // 建立实际的连接
  122. conn.connect();
  123. // 获取所有响应头字段
  124. Map<String, List<String>> map = conn.getHeaderFields();
  125. // 遍历所有的响应头字段
  126. // for (String key : map.keySet()) {
  127. // System.out.println(key + "--->" + map.get(key));
  128. // }
  129. // 定义 BufferedReader输入流来读取URL的响应
  130. in = new BufferedReader(new InputStreamReader(
  131. conn.getInputStream(), CHARSET));
  132. String line;
  133. while ((line = in.readLine()) != null) {
  134. result += line;
  135. }
  136. } catch (Exception e) {
  137. System.out.println("发送GET请求出现异常!" + e);
  138. e.printStackTrace();
  139. }
  140. // 使用finally块来关闭输入流
  141. finally {
  142. try {
  143. if (in != null) {
  144. in.close();
  145. }
  146. } catch (Exception e2) {
  147. e2.printStackTrace();
  148. }
  149. }
  150. return result;
  151. }
  152. public void setHeader(String key, String value) {
  153. headers.put(key, value);
  154. }
  155. public URL getUrl() {
  156. return url;
  157. }
  158. public void setUrl(URL url) {
  159. this.url = url;
  160. }
  161. public HttpURLConnection getConn() {
  162. return conn;
  163. }
  164. public void setConn(HttpURLConnection conn) {
  165. this.conn = conn;
  166. }
  167. public Map<String, String> getHeaders() {
  168. return headers;
  169. }
  170. public void setHeaders(Map<String, String> headers) {
  171. this.headers = headers;
  172. }
  173. public static void main(String[] args) throws Exception {
  174. HttpUtil httpUtil = new HttpUtil("http://ehr.yihu.com/wlyy/gc/accesstoken");
  175. Map map = new HashMap();
  176. map.put("appid", "915d0345-5b1d-11e6-8344-fa163e8aee56");
  177. map.put("appSecret", "915d0345-5b1d-11e6-8344-fa163e8aee52");
  178. String resp = httpUtil.post(map);
  179. System.out.println(resp);
  180. }
  181. }