package com.zskk.control; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.jfinal.core.Controller; import com.jfinal.kit.PropKit; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; import com.jfinal.plugin.activerecord.dialect.OracleDialect; import com.jfinal.plugin.druid.DruidPlugin; import com.zskk.tools.SSLSocketClient; import okhttp3.FormBody; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class ViewController extends Controller { private static final MediaType JSON_CODE = MediaType.get("application/json; charset=utf-8"); private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient(); /** * 在被连接数据库执行sql语句 */ public void executeSql() { try { List d = Db.use("connected").find(this.getPara("sqlstr")); this.renderJson(d); } catch (Exception e) { // TODO: handle exception this.renderText(e.toString()); } } public void testget() { try { Map map = new HashMap(); map.put("institution_id", "73090004"); map.put("num","30"); String content = postWithParameters("https://117.156.67.61:9600/butt/getExam", map); // JSONObject jsonObject = JSON.parseObject(content); // if (!jsonObject.getString("msg").equals("success")) { //// return null; // } // JSONArray jsonArray = JSON.parseArray(jsonObject.getString("data")); // return jsonArray; this.renderText(content); } catch (Exception e) { // TODO: handle exception this.renderText(e.toString()); } } public static String postWithParameters(String url, Map map) { OkHttpClient client = new OkHttpClient.Builder() .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager()) .hostnameVerifier(SSLSocketClient.getHostnameVerifier()) .build(); FormBody.Builder formbody = new FormBody.Builder(); for (Map.Entry entry : map.entrySet()) { formbody.add(entry.getKey(), entry.getValue()); } RequestBody requestBody = formbody.build(); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String content = response.body().string(); return content; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public static DruidPlugin createConnectedDruidPlugin() { return new DruidPlugin(PropKit.get("jdbcUrl_connected"), PropKit.get("user_connected"),PropKit.get("password_connected").trim()); } public void testConn() { try { DruidPlugin druidPluginConnected = createConnectedDruidPlugin(); druidPluginConnected.start(); // 配置ActiveRecord插件 ActiveRecordPlugin arpConnected = new ActiveRecordPlugin("connected", druidPluginConnected); arpConnected.setDialect(new OracleDialect()); arpConnected.start(); } catch (Exception e) { // TODO: handle exception this.renderText(e.toString()); }} /** * post请求 * @param url-请求地址 * @param map-参数集合 * @return */ private static String doPost(String url, Map map) { FormBody.Builder builder = new FormBody.Builder(); for (String key : map.keySet()) { builder.add(key, map.get(key)); } RequestBody formBody = builder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); try (Response response = OKHTTP_CLIENT.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String content = response.body().string(); return content; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /** * 日期字符串格式转换 * @param dateStr * @return */ private Date parseStringToDate(String dateStr) { if (dateStr == null) { return new Date(); } SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = sdf.parse(dateStr); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return date; } public static void main(String[] args) { Map paramsMap=new HashMap(); paramsMap.put("institution_id", "44100001"); String contentString = doPost("https://risserver3.pacsonline.cn/butt/getExam/butt/getExam", paramsMap); System.out.println(contentString); } }