package com.qiwu.chatbotbackend.processing; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.entity.Solution; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; public class SimpleUtil { /** * 将后台返回的数据转成json格式 */ public static String toJson(Solution ans) { JSONObject jobj = new JSONObject(); jobj.put("solution", ans.getAnswer()); jobj.put("status", ans.getStatus()); jobj.put("process", ans.getNext()); return jobj.toString(); } public static String toJson(String returnStr) { JSONObject jobj = new JSONObject(); jobj.put("returnString", returnStr); return jobj.toString(); } /** * 当请求中的数据是json格式时,该函数用于提取request中的json数据 * * @param request * @return json字符串 */ public static String readJSONString(HttpServletRequest request) { StringBuffer json = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { json.append(line); } } catch (IOException e) { e.printStackTrace(); } return json.toString(); } public static JSONObject getMethod(String url, String auth){ CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse resp = null; String respContent = null; String keyName = null; JSONObject jobj = new JSONObject(); try { HttpGet httpGet = new HttpGet(url); if(auth != null){ httpGet.setHeader("Authorization", auth); } resp = client.execute(httpGet); if (resp.getStatusLine().getStatusCode() == 200) { HttpEntity he = resp.getEntity(); respContent = EntityUtils.toString(he, "UTF-8"); jobj = JSONObject.parseObject(respContent); jobj.put("get_status", 1); jobj.put("status_code", 200); } else { jobj.put("get_status", 0); jobj.put("status_code", resp.getStatusLine().getStatusCode()); jobj.put("parameter", "接收请求的程序出现异常"); } } catch (Exception e) { e.printStackTrace(); jobj.put("status_code", 0); jobj.put("get_status", 0); }finally { try { if (resp != null) { resp.close(); } if (client != null) { client.close(); } }catch(IOException e){ e.printStackTrace(); } } return jobj; } /** * 通过get方法传递参数时需要对参数的编码进行转换 */ public static String convert2Utf8(String s) { if (s == null) return null; try { s = new String(s.getBytes("iso-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; } public static long transferToMillisecond(String keyword) throws ParseException { long time; System.out.println(keyword); String timeSeries[] = {"时", "分", "秒"}; for (String f : timeSeries) { if (!keyword.contains(f)) keyword += "0" + f; } SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d日H时m分s秒"); time = format.parse(keyword).getTime(); return time; } public static String transferToStandardTime(long time) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy年M月d日H时m分s秒"); Date date = new Date(time); return format.format(date); } public static boolean isInteger(String str){ return true; } public static JSONObject postJSON(String url, JSON data, String auth) throws UnsupportedEncodingException { JSONObject jobj = new JSONObject(); HttpPost httpPost = new HttpPost(url); CloseableHttpClient client = HttpClients.createDefault(); String respContent = null; String keyName = null; StringEntity entity = new StringEntity(data.toJSONString(), "utf-8"); entity.setContentType("application/json; charset=utf-8"); httpPost.setEntity(entity); if(auth != null){ httpPost.setHeader("Authorization", auth); } try { HttpResponse resp = client.execute(httpPost); jobj.put("status_code", resp.getStatusLine().getStatusCode()); if (resp.getStatusLine().getStatusCode() == 200) { HttpEntity he = resp.getEntity(); respContent = EntityUtils.toString(he, "UTF-8"); jobj = JSONObject.parseObject(respContent); jobj.put("post_status", 1); jobj.put("status_code", 200); } else { jobj.put("post_status", 0); jobj.put("status_code", resp.getStatusLine().getStatusCode()); jobj.put("parameter", "接收请求的程序出现异常:" + resp.getStatusLine().getReasonPhrase()); } } catch (Exception e) { jobj.put("status_code", 0); jobj.put("post_status", 0); e.printStackTrace(); } return jobj; } public static JSONObject postFORM(String url, List pairList) throws UnsupportedEncodingException { HttpPost httpPost = new HttpPost(url); CloseableHttpClient client = HttpClients.createDefault(); String respContent = null; String keyName = null; UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairList, "utf-8"); entity.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(entity); JSONObject jobj = new JSONObject(); try { System.out.println("url为:" + url); HttpResponse resp = client.execute(httpPost); if (resp.getStatusLine().getStatusCode() == 200) { HttpEntity he = resp.getEntity(); respContent = EntityUtils.toString(he, "UTF-8"); jobj = JSONObject.parseObject(respContent); jobj.put("post_status", 1); } else { jobj.put("post_status", 0); jobj.put("error_message", "目标程序处理失败:" + url + ", 错误码:" + resp.getStatusLine().getStatusCode()); } } catch (Exception e) { jobj.put("error_message", "提交表单时出错:" + e.getMessage()); jobj.put("post_status", 0); } return jobj; } public static String randStr(String strList[]){ String reply; int randNum = (int) (Math.random() * strList.length); reply = strList[randNum]; return reply; } public static String randStr(List strList){ String reply; int randNum = (int) (Math.random() * strList.size()); reply = strList.get(randNum); return reply; } }