Commit 5a317397 authored by wdy's avatar wdy

填写问卷

parent 07023db0
...@@ -6,6 +6,13 @@ import com.ruoyi.domain.TestRecords; ...@@ -6,6 +6,13 @@ import com.ruoyi.domain.TestRecords;
import com.ruoyi.mapper.TestRecordsMapper; import com.ruoyi.mapper.TestRecordsMapper;
import com.ruoyi.service.TestRecordsService; import com.ruoyi.service.TestRecordsService;
import okhttp3.Request; import okhttp3.Request;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -13,6 +20,9 @@ import java.io.*; ...@@ -13,6 +20,9 @@ import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
@Transactional @Transactional
...@@ -24,17 +34,6 @@ public class TestRecordsServiceImpl extends ServiceImpl<TestRecordsMapper, TestR ...@@ -24,17 +34,6 @@ public class TestRecordsServiceImpl extends ServiceImpl<TestRecordsMapper, TestR
JSONObject jsonObject = getInverseParamByUrl("http://10.12.48.78:8090/DescribeProjectTestResult"); JSONObject jsonObject = getInverseParamByUrl("http://10.12.48.78:8090/DescribeProjectTestResult");
} }
// public static void main(String[] args) throws IOException {
// JSONObject jsonObject = getInverseParamByUrl("http://10.12.48.78:8090/DescribeProjectTestResult");
//
// System.out.println("jsonObject = " + jsonObject);
// }
/**
* 通过API获取反参
* @param apiUrl
* @return
* @throws IOException
*/
public static JSONObject getInverseParamByUrl(String apiUrl) throws IOException { public static JSONObject getInverseParamByUrl(String apiUrl) throws IOException {
// 调用请求 // 调用请求
URL url = new URL(apiUrl); URL url = new URL(apiUrl);
...@@ -54,62 +53,104 @@ public class TestRecordsServiceImpl extends ServiceImpl<TestRecordsMapper, TestR ...@@ -54,62 +53,104 @@ public class TestRecordsServiceImpl extends ServiceImpl<TestRecordsMapper, TestR
return jsonObject; return jsonObject;
} }
public static void main(String[] args) throws UnsupportedEncodingException { public static String doPost(String url, JSONObject json){
// 创建一个Request对象
// Request request = new Request();
// request.setParam1("project_items;1");
// request.setParam2("ALL");
//
// // 将Request对象转换为JSON字符串
// Gson gson = new GsonBuilder().create();
// String jsonRequest = gson.toJson(request);
try { try {
String apiUrl = "http://10.12.48.78:8090/DescribeProjectTestResult"; // 第三方URL CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String param1 = "project_items;1"; HttpPost post = new HttpPost(url);
String param2 = "ALL";
// 首先Header部分需要设定字符集为:uft-8
String postData = String.format("param1=%s&param2=%s", post.addHeader("Content-Type", "application/json;charset=utf-8");
URLEncoder.encode(param1, "UTF-8"), // 此处也需要设定
URLEncoder.encode(param2, "UTF-8")); post.setHeader("Accept", "*/*");
post.setHeader("Accept-Encoding", "gzip, deflate, br");
URL url = new URL(apiUrl); post.setHeader("Connection", "keep-alive");
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36");
conn.setRequestMethod("POST"); post.setEntity(new StringEntity(json.toString(), Charset.forName("UTF-8"))); //设置请求参数
conn.setDoOutput(true); HttpResponse response = httpClient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
// 设置请求体 if (HttpStatus.SC_OK == statusCode){
DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream()); //返回String
outputStream.writeBytes(postData); String res = EntityUtils.toString(response.getEntity());
outputStream.flush(); System.out.println(res);
outputStream.close(); return res;
}else{
int responseCode = conn.getResponseCode(); return "调用POST请求失败";
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String responseBody = response.toString();
JSONObject jsonObject = JSONObject.parseObject(responseBody);
System.out.println("Response body: " + responseBody);
System.out.println("jsonObject = " + jsonObject);
} else {
System.out.println("Request failed. Response code: " + responseCode);
} }
conn.disconnect(); } catch (Exception e) {
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return "调用POST请求失败";
} }
} }
// public static void main(String[] args) {
// Map<String, Object> map = new HashMap<>();
// map.put("id", "project_items;1");
// map.put("verbose", "ALL");
// JSONObject json = new JSONObject(map);
//
// //以post形式请求接口
// String result = doPost("http://10.12.48.78:8090/DescribeProjectTestResult", json);
// JSONObject jsonObject = JSONObject.parseObject(result);
// String data = jsonObject.get("data").toString();
//
// System.out.println("data = " + data);
// }
// public static void main(String[] args) {
//
// try {
// String apiUrl = "http://10.12.48.78:8090/DescribeProjectTestResult"; // 第三方URL
//
//
// URL url = new URL(apiUrl);
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setRequestMethod("POST");
// conn.setDoInput(true);
// conn.setDoOutput(true);
//
// // 参数设置 ||
// conn.setRequestProperty("Content-Type","application/json");
// conn.connect();
//
// // 设置请求体
// OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
// JSONObject parm = new JSONObject();
// parm.put("id","project_items;1");
// parm.put("verbose","ALL");
// writer.write(URLEncoder.encode(parm.toString(),"UTF-8"));
// writer.flush();
//
//
// int responseCode = conn.getResponseCode();
// if (responseCode == HttpURLConnection.HTTP_OK) {
// BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
// String inputLine;
// StringBuilder response = new StringBuilder();
//// StringBuffer response = new StringBuffer();
//
// while ((inputLine = in.readLine()) != null) {
// response.append(inputLine);
// }
// in.close();
// conn.disconnect();
//
// String result = response.toString();
// System.out.println("result = " + result);
//
//// String responseBody = response.toString();
//// JSONObject jsonObject = JSONObject.parseObject(responseBody);
//// System.out.println("Response body: " + responseBody);
//// System.out.println("jsonObject = " + jsonObject);
// } else {
// System.out.println("Request failed. Response code: " + responseCode);
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment