package com.whyc.util; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; /** * */ public class HttpUtil { public static final String VIDEO_URL = "https://open.ys7.com/api/lapp/token/get"; public static final String VIDEO_REQUEST_BODY = "appKey=07457eaa64a546a2bb016635fe1e744d&appSecret=8438b941b7e129e2ba0689759326ad56"; public static String sendGet(String url, String param) { String result = ""; BufferedReader in = null; try { String urlNameString = url + "?" + param; URL realUrl = new URL(urlNameString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map> map = connection.getHeaderFields(); // 遍历所有的响应头字段 /*for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); }*/ // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); 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; } /** * get方式的http请求 * * @param httpUrl 请求地址 * @return 返回结果 */ public static String doGet(String httpUrl, String queryParams) { HttpURLConnection connection = null; InputStream inputStream = null; BufferedReader bufferedReader = null; String result = null;// 返回结果字符串 try { // 创建远程url连接对象 URL url = new URL(new StringBuffer(httpUrl).append("?").append(queryParams).toString()); // 通过远程url连接对象打开一个连接,强转成httpURLConnection类 connection = (HttpURLConnection) url.openConnection(); // 设置连接方式:get connection.setRequestMethod("GET"); // 设置连接主机服务器的超时时间:15000毫秒 connection.setConnectTimeout(5000); // 设置读取远程返回的数据时间:60000毫秒 connection.setReadTimeout(6000); // 发送请求 connection.connect(); // 通过connection连接,获取输入流 if (connection.getResponseCode() == 200) { inputStream = connection.getInputStream(); // 封装输入流,并指定字符集 bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); // 存放数据 StringBuilder sbf = new StringBuilder(); String temp; while ((temp = bufferedReader.readLine()) != null) { sbf.append(temp); sbf.append(System.getProperty("line.separator")); } result = sbf.toString(); } } catch (IOException e) { e.printStackTrace(); } finally { // 关闭资源 if (null != bufferedReader) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != inputStream) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { connection.disconnect();// 关闭远程连接 } } return result; } /** * post方式的http请求 * * @param httpUrl 请求地址 * @return 返回结果 */ public static String doPost(String httpUrl,String requestBody) { HttpURLConnection connection = null; InputStream inputStream = null; BufferedReader bufferedReader = null; String result = null;// 返回结果字符串 try { // 创建远程url连接对象 URL url = new URL(new StringBuffer(httpUrl).toString()); // 通过远程url连接对象打开一个连接,强转成httpURLConnection类 connection = (HttpURLConnection) url.openConnection(); // 设置连接方式:post connection.setRequestMethod("POST"); // 设置连接主机服务器的超时时间:15000毫秒 connection.setConnectTimeout(5000); // 设置读取远程返回的数据时间:60000毫秒 connection.setReadTimeout(6000); //请求体 connection.setDoOutput(true); OutputStream outputStream = connection.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(outputStream,"UTF-8"); osw.write(requestBody); osw.flush(); outputStream.close(); // 发送请求 connection.connect(); // 通过connection连接,获取输入流 if (connection.getResponseCode() == 200) { inputStream = connection.getInputStream(); // 封装输入流,并指定字符集 bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); // 存放数据 StringBuilder sbf = new StringBuilder(); String temp; while ((temp = bufferedReader.readLine()) != null) { sbf.append(temp); sbf.append(System.getProperty("line.separator")); } result = sbf.toString(); } } catch (IOException e) { e.printStackTrace(); } finally { // 关闭资源 if (null != bufferedReader) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != inputStream) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { connection.disconnect();// 关闭远程连接 } } return result; } /** * 将map型转为请求参数型 * * @param data * @return */ public static String urlencode(Map data) { StringBuilder sb = new StringBuilder(); for (Map.Entry i : data.entrySet()) { try { sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } String result = sb.toString(); result = result.substring(0, result.lastIndexOf("&")); return result; } }