| | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.GsonBuilder; |
| | | import com.google.gson.JsonSyntaxException; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.dto.Response4Http; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.io.*; |
| | | import java.lang.reflect.Type; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | |
| | | public class HttpUtil { |
| | | |
| | |
| | | return getSession().getServletContext(); |
| | | } |
| | | |
| | | /* |
| | | * 获取将对象转换成json格式 |
| | | /** |
| | | * get方式的http请求 |
| | | * |
| | | * @param httpUrl 请求地址 |
| | | * @return 返回结果 |
| | | */ |
| | | public static String toJson(Object obj){ |
| | | Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); |
| | | return gson.toJson(obj); |
| | | } |
| | | |
| | | public static String chageDateToString(Date time,String type){ |
| | | String msg = ""; |
| | | SimpleDateFormat sdf = new SimpleDateFormat(type); |
| | | msg = sdf.format(time); |
| | | return msg; |
| | | public static Response<String> doGet(String httpUrl, String queryParams) { |
| | | HttpURLConnection connection = null; |
| | | InputStream inputStream = null; |
| | | BufferedReader bufferedReader = null; |
| | | String result = null;// 返回结果字符串 |
| | | Response<String> response = new Response<>(); |
| | | 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(); |
| | | response.set(1,result); |
| | | } |
| | | } catch (IOException e) { |
| | | response.set(0); |
| | | String message = e.getMessage(); |
| | | response.setMsg(message); |
| | | } 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 response; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * |
| | | * @param datetype 需要解析的日期的格式如:"yyyy-MM-dd HH:mm:ss" |
| | | * @return 得到对应的gson对象 |
| | | */ |
| | | public static Gson getGson(String datetype){ |
| | | return new GsonBuilder().setDateFormat(datetype).create(); |
| | | public static Response<String> doPost(String httpUrl, String requestBody) { |
| | | HttpURLConnection connection = null; |
| | | InputStream inputStream = null; |
| | | OutputStream outputStream =null; |
| | | BufferedReader bufferedReader = null; |
| | | String result = null;// 返回结果字符串 |
| | | Response<String> response = new Response<>(); |
| | | 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); |
| | | //设置contentType固定为application/json |
| | | connection.setRequestProperty("Content-Type","application/json"); |
| | | //请求体 |
| | | connection.setDoOutput(true); |
| | | outputStream = connection.getOutputStream(); |
| | | OutputStreamWriter osw = new OutputStreamWriter(outputStream,"UTF-8"); |
| | | |
| | | if(requestBody != null) { |
| | | osw.write(requestBody); |
| | | osw.flush(); |
| | | } |
| | | // 发送请求 |
| | | 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(); |
| | | response.set(1); |
| | | response.setData(result); |
| | | } |
| | | } catch (IOException e) { |
| | | String message = e.getMessage(); |
| | | response.set(0); |
| | | response.setMsg(message); |
| | | } 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();// 关闭远程连接 |
| | | } |
| | | if (null != outputStream) { |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return response; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取默认的gson对象 |
| | | * 将map型转为请求参数型 |
| | | * |
| | | * @param data |
| | | * @return |
| | | */ |
| | | public static Gson getGson(){ |
| | | return new Gson(); |
| | | } |
| | | |
| | | |
| | | public static <T> T getObject(String jsonstring,Type listtype){ |
| | | Gson gson=new Gson(); |
| | | T t=null; |
| | | try { |
| | | t=gson.fromJson(jsonstring, listtype); |
| | | } catch (JsonSyntaxException e) { |
| | | e.printStackTrace(); |
| | | public static String urlEncode(Map<String, ?> data) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (Map.Entry<String, ?> i : data.entrySet()) { |
| | | try { |
| | | sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&"); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return t; |
| | | String result = sb.toString(); |
| | | result = result.substring(0, result.lastIndexOf("&")); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 用于Video 的Http接口请求 |
| | | * 执行GET请求,传入Map参数和Url,获取响应 |
| | | * 这个对原始的doGet方法进行了调用,对结果进行后续逻辑再处理,属于定制化的接口了. |
| | | */ |
| | | public static Response4Http doGet(String httpUrl, Map<String, ?> params) { |
| | | Response4Http responseResult = new Response4Http<>(); |
| | | |
| | | String queryParams = urlEncode(params); |
| | | Response response = HttpUtil.doGet(httpUrl, queryParams); |
| | | Integer httpResponseCode = response.getCode(); |
| | | String responseJson = (String) response.getData(); |
| | | if(httpResponseCode == 1) { //请求成功 |
| | | Response4Http responseHttp = JsonUtil.getGson().fromJson(responseJson, Response4Http.class); |
| | | if(responseHttp.getCode() == 0 ) { //接口返回状态码为成功 |
| | | //返回信息有 data属性 |
| | | if(responseHttp.getData()!=null) { |
| | | return responseResult.setII(1,true,responseHttp.getData(),null); |
| | | } |
| | | //返回信息有 status属性 |
| | | else if(responseHttp.getStatus() !=null){ |
| | | return responseResult.setStatus(1,true,responseHttp.getStatus()); |
| | | } |
| | | //返回信息有 result属性 |
| | | else if(responseHttp.getResult() !=null){ |
| | | responseResult.setResult(responseHttp.getResult()); |
| | | return responseResult.set(1,true); |
| | | } |
| | | else{ |
| | | return responseResult.set(1,true,"请求成功且返回数据正常,无数据信息"); |
| | | } |
| | | }else{ //接口返回状态码为失败 |
| | | // 返回信息有 msg属性 |
| | | return responseResult.set(1,false,responseHttp.getMsg()); |
| | | } |
| | | }else{ //请求失败 |
| | | return responseResult.set(0,response.getData(),response.getMsg()); |
| | | } |
| | | |
| | | } |
| | | } |