123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- package com.yihu.platform.utils;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- /**
- * http工具类
- *
- * @author jcl
- * @company yihu.com
- * @since 2014-12-29
- */
- public class HttpUtil {
- private URL url;
- private HttpURLConnection conn;
- private Map<String, String> headers = new HashMap<String, String>();//头信息
- private static final String CHARSET = "utf-8";
- private static final String POST = "POST";
- private static final String GET = "GET";
- public HttpUtil(URL url) {
- this.url = url;
- }
- public HttpUtil(String url) throws MalformedURLException {
- this.url = new URL(url);
- }
- public String post(Map<String, String> param) {
- PrintWriter out = null;
- BufferedReader in = null;
- String result = "";
- try {
- // 打开和URL之间的连接
- conn = (HttpURLConnection) url.openConnection();
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.setUseCaches(false);
- conn.setRequestMethod(POST);
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="+CHARSET);
- for (Entry<String, String> entry : headers.entrySet()) {
- conn.setRequestProperty(entry.getKey(), entry.getValue());
- }
- // 发送POST请求必须设置如下两行
- // 获取URLConnection对象对应的输出流
- out = new PrintWriter(conn.getOutputStream());
- // 发送请求参数
- StringBuilder paramStr = new StringBuilder();
- if (param != null) {
- for (Entry entry : param.entrySet()) {
- paramStr.append(entry.getKey()).append("=");
- if (StringUtil.isNotEmpty(entry.getValue())) paramStr.append(entry.getValue());
- paramStr.append("&");
- }
- if (paramStr.length() > 0) paramStr.deleteCharAt(paramStr.length() - 1);
- // System.out.println(paramStr);
- }
- out.print(paramStr.toString());
- // flush输出流的缓冲
- out.flush();
- // 定义BufferedReader输入流来读取URL的响应
- in = new BufferedReader(
- new InputStreamReader(conn.getInputStream(), CHARSET));
- String line;
- while ((line = in.readLine()) != null) {
- result += line;
- }
- } catch (Exception e) {
- System.out.println("发送 POST 请求出现异常!" + e);
- e.printStackTrace();
- }
- //使用finally块来关闭输出流、输入流
- finally {
- try {
- if (out != null) {
- out.close();
- }
- if (in != null) {
- in.close();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- return result;
- }
- public String get(Map<String, String> param) {
- String result = "";
- BufferedReader in = null;
- try {
- StringBuilder paramStr = new StringBuilder();
- if (param != null) {
- for (Entry entry : param.entrySet()) {
- paramStr.append(entry.getKey()).append("=");
- if (StringUtil.isNotEmpty(entry.getValue())) paramStr.append(entry.getValue());
- paramStr.append("&");
- }
- if (paramStr.length() > 0) paramStr.deleteCharAt(paramStr.length() - 1);
- // System.out.println(paramStr);
- }
- String urlNameString = url.toString() + "?" + paramStr.toString();
- System.out.println("请求串------->" + urlNameString);
- URL realUrl = new URL(urlNameString);
- // 打开和URL之间的连接
- conn = (HttpURLConnection) realUrl.openConnection();
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.setUseCaches(false);
- conn.setRequestMethod(GET);
- for (Entry<String, String> entry : headers.entrySet()) {
- conn.setRequestProperty(entry.getKey(), entry.getValue());
- }
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="+CHARSET);
- // 建立实际的连接
- conn.connect();
- // 获取所有响应头字段
- Map<String, List<String>> map = conn.getHeaderFields();
- // 遍历所有的响应头字段
- // for (String key : map.keySet()) {
- // System.out.println(key + "--->" + map.get(key));
- // }
- // 定义 BufferedReader输入流来读取URL的响应
- in = new BufferedReader(new InputStreamReader(
- conn.getInputStream(), CHARSET));
- String line;
- while ((line = in.readLine()) != null) {
- result += line;
- }
- } catch (Exception e) {
- System.out.println("发送GET请求出现异常!" + e);
- e.printStackTrace();
- }
- // 使用finally块来关闭输入流
- finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return result;
- }
- public void setHeader(String key, String value) {
- headers.put(key, value);
- }
- public URL getUrl() {
- return url;
- }
- public void setUrl(URL url) {
- this.url = url;
- }
- public HttpURLConnection getConn() {
- return conn;
- }
- public void setConn(HttpURLConnection conn) {
- this.conn = conn;
- }
- public Map<String, String> getHeaders() {
- return headers;
- }
- public void setHeaders(Map<String, String> headers) {
- this.headers = headers;
- }
- public static void main(String[] args) throws Exception {
- HttpUtil httpUtil = new HttpUtil("http://ehr.yihu.com/wlyy/gc/accesstoken");
- Map map = new HashMap();
- map.put("appid", "915d0345-5b1d-11e6-8344-fa163e8aee56");
- map.put("appSecret", "915d0345-5b1d-11e6-8344-fa163e8aee52");
- String resp = httpUtil.post(map);
- System.out.println(resp);
- }
- }
|