Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
vehicle-quality-review
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王飞
vehicle-quality-review
Commits
df61be58
Commit
df61be58
authored
Mar 21, 2024
by
wdy
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'wangdingyi' into 'dev'
填写问卷 See merge request
!214
parents
72533b60
3c932f4e
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
101 additions
and
60 deletions
+101
-60
TestRecordsServiceImpl.java
...n/java/com/ruoyi/service/impl/TestRecordsServiceImpl.java
+101
-60
No files found.
quality-review/src/main/java/com/ruoyi/service/impl/TestRecordsServiceImpl.java
View file @
df61be58
...
...
@@ -6,6 +6,13 @@ import com.ruoyi.domain.TestRecords;
import
com.ruoyi.mapper.TestRecordsMapper
;
import
com.ruoyi.service.TestRecordsService
;
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.transaction.annotation.Transactional
;
...
...
@@ -13,6 +20,9 @@ import java.io.*;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URLEncoder
;
import
java.nio.charset.Charset
;
import
java.util.HashMap
;
import
java.util.Map
;
@Transactional
...
...
@@ -24,17 +34,6 @@ public class TestRecordsServiceImpl extends ServiceImpl<TestRecordsMapper, TestR
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
{
// 调用请求
URL
url
=
new
URL
(
apiUrl
);
...
...
@@ -54,62 +53,104 @@ public class TestRecordsServiceImpl extends ServiceImpl<TestRecordsMapper, TestR
return
jsonObject
;
}
public
static
void
main
(
String
[]
args
)
throws
UnsupportedEncodingException
{
// 创建一个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);
public
static
String
doPost
(
String
url
,
JSONObject
json
){
try
{
String
apiUrl
=
"http://10.12.48.78:8090/DescribeProjectTestResult"
;
// 第三方URL
String
param1
=
"project_items;1"
;
String
param2
=
"ALL"
;
String
postData
=
String
.
format
(
"param1=%s¶m2=%s"
,
URLEncoder
.
encode
(
param1
,
"UTF-8"
),
URLEncoder
.
encode
(
param2
,
"UTF-8"
));
URL
url
=
new
URL
(
apiUrl
);
HttpURLConnection
conn
=
(
HttpURLConnection
)
url
.
openConnection
();
conn
.
setRequestMethod
(
"POST"
);
conn
.
setDoOutput
(
true
);
// 设置请求体
DataOutputStream
outputStream
=
new
DataOutputStream
(
conn
.
getOutputStream
());
outputStream
.
writeBytes
(
postData
);
outputStream
.
flush
();
outputStream
.
close
();
int
responseCode
=
conn
.
getResponseCode
();
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
);
CloseableHttpClient
httpClient
=
HttpClientBuilder
.
create
().
build
();
HttpPost
post
=
new
HttpPost
(
url
);
// 首先Header部分需要设定字符集为:uft-8
post
.
addHeader
(
"Content-Type"
,
"application/json;charset=utf-8"
);
// 此处也需要设定
post
.
setHeader
(
"Accept"
,
"*/*"
);
post
.
setHeader
(
"Accept-Encoding"
,
"gzip, deflate, br"
);
post
.
setHeader
(
"Connection"
,
"keep-alive"
);
// 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");
post
.
setEntity
(
new
StringEntity
(
json
.
toString
(),
Charset
.
forName
(
"UTF-8"
)));
//设置请求参数
HttpResponse
response
=
httpClient
.
execute
(
post
);
int
statusCode
=
response
.
getStatusLine
().
getStatusCode
();
if
(
HttpStatus
.
SC_OK
==
statusCode
){
//返回String
String
res
=
EntityUtils
.
toString
(
response
.
getEntity
());
System
.
out
.
println
(
res
);
return
res
;
}
else
{
return
"调用POST请求失败"
;
}
conn
.
disconnect
();
}
catch
(
IOException
e
)
{
}
catch
(
Exception
e
)
{
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();
// }
// }
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment