- //获取post响应,暂无header设置
- public JSONObject getHttpResponseEntityByJson(CloseableHttpClient httpClient, HttpPost httpPost) throws ClientProtocolException, IOException, SQLException, JSONException {
- //在请求中明确定义不要进行压缩
- // httpPost.setHeader("Accept-Encoding", "identity");
- HttpResponse response = null;//创建响应对象
- long date_size = 0;//用于存放数据大小
- String api_name = getApiName(httpPost);//获取接口名称
- String type = getProtocolType(httpPost);//获取协议类型
- String host_name = getHostName(httpPost);//获取hsot主机名称
- Date start = getDate();//记录开始时间
- response = httpClient.execute(httpPost);//获取响应
- Date end = getDate();//记录结束时间
- double elapsed_time= outputTimeDiffer(start, end, "接口:" + api_name);//获取响应耗时
- int status = response.getStatusLine().getStatusCode();//获取响应状态
- if (status == HttpStatus.SC_OK) {//判断一下返回状态
- output("host:" + host_name, "接口:"+ api_name + "请求成功!");
- } else {
- output(response.getStatusLine().getStatusCode() + LINE + "请求失败!");
- }
- HttpEntity entity = response.getEntity();//获取响应实体
- data_size = entity.getContentLength();//获取相应数据大小
- if (data_size == -1) {//如果为-1,则重置date_size
- date_size = 0;
- }
- String content = EntityUtils.toString(entity);//解析响应
- if (date_size == 0) {//如果被重置或者没有获取到,则date_size等于解析string大小
- date_size = content.length();
- }
- JSONObject jsonObject = new JSONObject(content);//转换json
- LocalMySql.getInstance().saveApiTestDate(host_name, api_name, date_size, elapsed_time, status, type);
- // httpClient.close();//关闭客户端
- return jsonObject;
- }
- //获取get接口响应,暂无header设置
- public JSONObject getHttpResponseEntityByJson(CloseableHttpClient httpClient, HttpGet httpGet) throws ClientProtocolException, IOException, JSONException, SQLException {
- //在请求中明确定义不要进行压缩
- // httpGet.setHeader("Accept-Encoding", "identity");
- HttpResponse response = null;
- long date_size = 0;
- String api_name = getApiName(httpGet);
- String type = getProtocolType(httpGet);
- Date start = getDate();
- response = httpClient.execute(httpGet);
- Date end = getDate();
- double elapsed_time= outputTimeDiffer(start, end, "接口:" + api_name);
- String host_name = getHostName(httpGet);
- int status = response.getStatusLine().getStatusCode();
- if (status == HttpStatus.SC_OK) {//判断一下返回状态
- output("host:" + host_name, "接口:"+ api_name + "请求成功!");
- } else {
- output(response.getStatusLine().getStatusCode() + LINE + "请求失败!");
- }
- HttpEntity entity = response.getEntity();
- data_size = entity.getContentLength();
- if (data_size == -1) {
- date_size = 0;
- }
- String content = EntityUtils.toString(entity);
- if (date_size == 0) {
- date_size = content.length();
- }
- JSONObject jsonObject = new JSONObject(content);
- LocalMySql.getInstance().saveApiTestDate(host_name, api_name, date_size, elapsed_time, status, type);
- // httpClient.close();
- return jsonObject;
- }
- //获取post响应,暂无header设置 public JSONObject getHttpResponseEntityByJson(CloseableHttpClient httpClient, HttpPost httpPost) throws ClientProtocolException, IOException, SQLException, JSONException { //在请求中明确定义不要进行压缩 // httpPost.setHeader("Accept-Encoding", "identity"); HttpResponse response = null;//创建响应对象 long date_size = 0;//用于存放数据大小 String api_name = getApiName(httpPost);//获取接口名称 String type = getProtocolType(httpPost);//获取协议类型 String host_name = getHostName(httpPost);//获取hsot主机名称 Date start = getDate();//记录开始时间 response = httpClient.execute(httpPost);//获取响应 Date end = getDate();//记录结束时间 double elapsed_time= outputTimeDiffer(start, end, "接口:" + api_name);//获取响应耗时 int status = response.getStatusLine().getStatusCode();//获取响应状态 if (status == HttpStatus.SC_OK) {//判断一下返回状态 output("host:" + host_name, "接口:"+ api_name + "请求成功!"); } else { output(response.getStatusLine().getStatusCode() + LINE + "请求失败!"); } HttpEntity entity = response.getEntity();//获取响应实体 data_size = entity.getContentLength();//获取相应数据大小 if (data_size == -1) {//如果为-1,则重置date_size date_size = 0; } String content = EntityUtils.toString(entity);//解析响应 if (date_size == 0) {//如果被重置或者没有获取到,则date_size等于解析string大小 date_size = content.length(); } JSONObject jsonObject = new JSONObject(content);//转换json LocalMySql.getInstance().saveApiTestDate(host_name, api_name, date_size, elapsed_time, status, type); // httpClient.close();//关闭客户端 return jsonObject; } //获取get接口响应,暂无header设置 public JSONObject getHttpResponseEntityByJson(CloseableHttpClient httpClient, HttpGet httpGet) throws ClientProtocolException, IOException, JSONException, SQLException { //在请求中明确定义不要进行压缩 // httpGet.setHeader("Accept-Encoding", "identity"); HttpResponse response = null; long date_size = 0; String api_name = getApiName(httpGet); String type = getProtocolType(httpGet); Date start = getDate(); response = httpClient.execute(httpGet); Date end = getDate(); double elapsed_time= outputTimeDiffer(start, end, "接口:" + api_name); String host_name = getHostName(httpGet); int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) {//判断一下返回状态 output("host:" + host_name, "接口:"+ api_name + "请求成功!"); } else { output(response.getStatusLine().getStatusCode() + LINE + "请求失败!"); } HttpEntity entity = response.getEntity(); data_size = entity.getContentLength(); if (data_size == -1) { date_size = 0; } String content = EntityUtils.toString(entity); if (date_size == 0) { date_size = content.length(); } JSONObject jsonObject = new JSONObject(content); LocalMySql.getInstance().saveApiTestDate(host_name, api_name, date_size, elapsed_time, status, type); // httpClient.close(); return jsonObject; }
中间使用了本地数据库,将收集的信息存入本地数据库里面,以便以后统计。代码如下:
[java] view plain copy print?
- //保存接口测试数据的方法
- public void saveApiTestDate(String host_name, String api_name, long date_size, double elapsed_time, int status, String type) throws SQLException {
- getConnection();
- if (!connection.isClosed()) {
- outputSuccess();
- Statement statement = connection.createStatement();
- String sql = "INSERT INTO api_result (host_name,api_name,date_size,elapsed_time,status,type) VALUES ('"+host_name+"','"+api_name+"',"
- +date_size+","+ elapsed_time + "," + status + ",'" + type +"');";
- // output(sql);
- statement.executeUpdate(sql);
- }
- }
//保存接口测试数据的方法 public void saveApiTestDate(String host_name, String api_name, long date_size, double elapsed_time, int status, String type) throws SQLException { getConnection(); if (!connection.isClosed()) { outputSuccess(); Statement statement = connection.createStatement(); String sql = "INSERT INTO api_result (host_name,api_name,date_size,elapsed_time,status,type) VALUES ('"+host_name+"','"+api_name+"'," +date_size+","+ elapsed_time + "," + status + ",'" + type +"');"; // output(sql); statement.executeUpdate(sql); } }
中间用到了一些自己封装的方法:
[java] view plain copy print?
- //获取json数据中的相应值
- public String getJsonValue(String jsonString, String key) throws JSONException {
- String JsonValue = null;
- if (jsonString == null || jsonString.trim().length() < 1) {//排除为空和空格
- return null;
- }
- JSONObject obj1 = new JSONObject(jsonString);
- JsonValue = (String) obj1.getString(key);
- return JsonValue;//返回对应值
- }
- //获取json数据中的相应值
- public String getJsonValue(JSONObject jsonObject, String key) {
- String JsonValue = "··-·";
- try {
- JsonValue = (String) jsonObject.getString(key);
- } catch (JSONException e) {
- output("json数据错误!");
- output(e.toString());
- }
- return JsonValue;//返回对应值
- }
- //把json数据转化为参数
- public String changeJsonToArguments(JSONObject argument) {
- String one = argument.toString();
- String two ="?" + one.substring(1, one.length()-1).replace(",", "&").replace(":", "=").replace("\"", "");
- return two;
- }
- //获取接口名称
- public String getApiName(HttpGet httpGet) {
- String url = getUrl(httpGet);
- String apiName = url.substring(url.indexOf("m/") + 1);
- return apiName;
- }
- //获取接口名称
- public String getApiName(HttpPost httpPost) {
- String url = getUrl(httpPost);
- String apiName = url.substring(url.indexOf("m/") + 1);
- return apiName;
- }
- //获取host名称
- public String getHostName(HttpPost httpPost) {
- String url = getUrl(httpPost);
- String host = url.substring(url.indexOf("//") + 2, url.indexOf("m/") + 2);
- return host;
- }
- //获取host名称
- public String getHostName(HttpGet httpGet) {
- String url = getUrl(httpGet);
- String host = url.substring(url.indexOf("//") + 2, url.indexOf("m/") + 2);
- return host;
- }
- //获取url
- public String getUrl(HttpGet httpGet) {
- String uri = httpGet.getURI().toString();
- String url = uri.substring(0, uri.indexOf("?"));
- return url;
- }
- //获取url
- public String getUrl(HttpPost httpPost) {
- String url = httpPost.getURI().toString();
- if (url.contains("?")) {
- url = url.substring(0, url.indexOf("?"));
- }
- return url;
- }
- //获取协议类型
- public String getProtocolType(HttpGet httpGet) {
- String uri = httpGet.getURI().toString();
- String type = uri.substring(0, uri.indexOf("//") - 1);
- return type;
- }
- //获取协议类型
- public String getProtocolType(HttpPost httpPost) {
- String uri = httpPost.getURI().toString();
- String type = uri.substring(0, uri.indexOf("//") - 1);
- return type;
- }
- //输出json
- public void output(JSONObject jsonObject) {
- String info = jsonObject.toString().replaceAll(",", LINE);
- output(info);
- }