|
@@ -1,258 +1,141 @@
|
|
|
package com.itheima.file;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.itheima.file.pojo.FatherSon;
|
|
|
+import com.itheima.file.pojo.Org;
|
|
|
+import com.itheima.file.pojo.Person;
|
|
|
+import com.itheima.file.service.FatherSonService;
|
|
|
+import com.itheima.file.service.OrgService;
|
|
|
+import com.itheima.file.service.PersonService;
|
|
|
import com.itheima.file.util.HTTPHelper;
|
|
|
-import com.itheima.file.util.PostMethodUtils;
|
|
|
+import com.itheima.file.util.HttpClientUtils;
|
|
|
import com.itheima.file.util.SslUtils;
|
|
|
import org.apache.hc.client5.http.auth.AuthScope;
|
|
|
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
|
|
|
-import org.apache.hc.client5.http.classic.methods.HttpGet;
|
|
|
-import org.apache.hc.client5.http.classic.methods.HttpPost;
|
|
|
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
|
|
|
-import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
|
|
-import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
|
|
-import org.apache.hc.client5.http.impl.classic.HttpClients;
|
|
|
-import org.apache.hc.core5.http.io.entity.EntityUtils;
|
|
|
-import org.apache.hc.core5.http.io.entity.StringEntity;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
-import javax.net.ssl.TrustManager;
|
|
|
-import javax.net.ssl.X509TrustManager;
|
|
|
import java.net.URISyntaxException;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.time.LocalDateTime;
|
|
|
import java.util.Base64;
|
|
|
import java.util.LinkedHashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.List;
|
|
|
|
|
|
-// @RunWith(SpringRunner.class)
|
|
|
-// @SpringBootTest
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@SpringBootTest
|
|
|
public class ApiTestData {
|
|
|
|
|
|
- public static void main111(final String[] args) throws Exception {
|
|
|
- // 忽略SSL证书验证的TrustManager
|
|
|
- TrustManager[] trustAllCerts = new TrustManager[] {
|
|
|
- new X509TrustManager() {
|
|
|
- public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public void checkClientTrusted(
|
|
|
- java.security.cert.X509Certificate[] certs, String authType) {
|
|
|
- }
|
|
|
-
|
|
|
- public void checkServerTrusted(
|
|
|
- java.security.cert.X509Certificate[] certs, String authType) {
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
-
|
|
|
- // 创建请求体
|
|
|
+ @Autowired
|
|
|
+ private OrgService orgService;
|
|
|
+ @Autowired
|
|
|
+ private PersonService personService;
|
|
|
+ @Autowired
|
|
|
+ private FatherSonService fatherSonService;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testFSApi() {
|
|
|
+ String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/JTOA/getlist";
|
|
|
+ String username = "po_soap";
|
|
|
+ String password = "z12345678";
|
|
|
JSONObject reqBodyObj = new JSONObject();
|
|
|
reqBodyObj.put("app_id", "hcmcloud");
|
|
|
- reqBodyObj.put("table_name", ""); // 确保这里有一个有效的值
|
|
|
+ reqBodyObj.put("table_name", "USER_ORG_RELATION");
|
|
|
reqBodyObj.put("stringDate", "20210301");
|
|
|
reqBodyObj.put("condition", "1=1");
|
|
|
-
|
|
|
- // 设置Basic Authentication的凭证(用户名和密码)
|
|
|
- String username = "po_soap1";
|
|
|
- String password = "z123456789";
|
|
|
-
|
|
|
- final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
- credsProvider.setCredentials(
|
|
|
- new AuthScope("httpbin.org", 80),
|
|
|
- new UsernamePasswordCredentials(username, password.toCharArray()));
|
|
|
-
|
|
|
- // String baseAuth = getBaseAuth(username, password);
|
|
|
-
|
|
|
- try (final CloseableHttpClient httpclient = HttpClients.custom()
|
|
|
- .setDefaultCredentialsProvider(credsProvider)
|
|
|
- .build()) {
|
|
|
- // final HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/admin/123456");
|
|
|
- SslUtils.ignoreSsl();
|
|
|
- // D:\developer\jdk-17.0.10\lib\security
|
|
|
- final HttpPost httpget = new HttpPost("https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/DeleteAssHrtree");
|
|
|
- // final HttpGet httpget = new HttpGet("https://segmentfault.com/a/1190000042354971#item-9");
|
|
|
-
|
|
|
- // 设置请求体
|
|
|
- final StringEntity reqEntity = new StringEntity(reqBodyObj.toString(), StandardCharsets.UTF_8);
|
|
|
- httpget.setEntity(reqEntity);
|
|
|
- System.out.println("执行请求" + httpget.getMethod() + " " + httpget.getUri());
|
|
|
- try (final CloseableHttpResponse response = httpclient.execute(httpget)) {
|
|
|
- System.out.println("----------------------------------------");
|
|
|
- System.out.println(response.getCode() + " " + response.getReasonPhrase());
|
|
|
- System.out.println(EntityUtils.toString(response.getEntity()));
|
|
|
- }
|
|
|
- }
|
|
|
+ reqBodyObj.put("secretKey", "f3ac852124d2f991a3f63f2e33dae18b");
|
|
|
+ JSONObject jsonObject = HttpClientUtils.doPost(postUrl, reqBodyObj, username, password);
|
|
|
+ System.out.println(jsonObject);
|
|
|
+ List<FatherSon> list = (List<FatherSon>) jsonObject.get("data");
|
|
|
+ fatherSonService.saveBatch(list);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 401
|
|
|
- * @param args
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static void main4(String[] args) throws Exception {
|
|
|
+ @Test
|
|
|
+ public void testPersonApi() throws URISyntaxException {
|
|
|
// 设置Basic Authentication的凭证(用户名和密码)
|
|
|
- String username = "po_soap1";
|
|
|
- String password = "z123456789";
|
|
|
+ String username = "po_soap";
|
|
|
+ String password = "z12345678";
|
|
|
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
credsProvider.setCredentials(
|
|
|
new AuthScope("httpbin.org", 80),
|
|
|
new UsernamePasswordCredentials(username, password.toCharArray()));
|
|
|
|
|
|
- String baseAuth = getBaseAuth(username, password);
|
|
|
- // 计算基本认证编码
|
|
|
- // String auth = Base64.getEncoder().encodeToString(
|
|
|
- // ("po_soap1" + ":" + "z123456789").getBytes(StandardCharsets.UTF_8)
|
|
|
- // );
|
|
|
-
|
|
|
JSONObject reqBodyObj = new JSONObject();
|
|
|
reqBodyObj.put("app_id", "hcmcloud");
|
|
|
reqBodyObj.put("table_name", "");
|
|
|
- reqBodyObj.put("stringDate", "20210301");
|
|
|
+ reqBodyObj.put("stringDate", "19000101");
|
|
|
reqBodyObj.put("condition", "1=1");
|
|
|
reqBodyObj.put("secretKey", "f3ac852124d2f991a3f63f2e33dae18b");
|
|
|
|
|
|
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
|
|
|
- // header.put("Authorization", "Basic " + credsProvider);
|
|
|
- header.put("Authorization", baseAuth);
|
|
|
- String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/DeleteAssHrtree";
|
|
|
+ String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/GetAssHrTreeObj";
|
|
|
+
|
|
|
+ // 设置HTTP头部
|
|
|
+ LinkedHashMap<String, String> header = new LinkedHashMap<>();
|
|
|
+ // header.put("Authorization", auth);
|
|
|
+ header.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));
|
|
|
+
|
|
|
+ // header.put("Authorization", baseAuth);
|
|
|
+ header.put("Content-Type", "application/json");
|
|
|
+ String string = reqBodyObj.toString();
|
|
|
+ // 发送请求(这里假设HTTPHelper.sendPOST方法支持自定义头部)
|
|
|
try {
|
|
|
SslUtils.ignoreSsl();
|
|
|
- String result = HTTPHelper.sendPOST(postUrl, reqBodyObj.toString(), header, "UTF-8");
|
|
|
+ String result = HTTPHelper.sendPOST(postUrl, string, header, "UTF-8");
|
|
|
System.out.println(result);
|
|
|
+ // String 转 Map
|
|
|
+ JSONObject resultObj = JSONObject.parseObject(result);
|
|
|
+ List<Person> list = (List<Person>) resultObj.get("data");
|
|
|
+ System.out.println(list);
|
|
|
+ personService.saveBatch(list);
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 响应401
|
|
|
- * @param args
|
|
|
- * @throws URISyntaxException
|
|
|
- */
|
|
|
- public static void main44(String[] args) throws URISyntaxException {
|
|
|
+ @Test
|
|
|
+ public void testOrgApi() throws URISyntaxException {
|
|
|
// 设置Basic Authentication的凭证(用户名和密码)
|
|
|
- String username = "po_soap1";
|
|
|
- String password = "z123456789";
|
|
|
+ String username = "po_soap";
|
|
|
+ String password = "z12345678";
|
|
|
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
|
|
|
credsProvider.setCredentials(
|
|
|
new AuthScope("httpbin.org", 80),
|
|
|
new UsernamePasswordCredentials(username, password.toCharArray()));
|
|
|
|
|
|
- String baseAuth = getBaseAuth(username, password);
|
|
|
-
|
|
|
JSONObject reqBodyObj = new JSONObject();
|
|
|
reqBodyObj.put("app_id", "hcmcloud");
|
|
|
reqBodyObj.put("table_name", "");
|
|
|
- reqBodyObj.put("stringDate", "20210301");
|
|
|
+ reqBodyObj.put("stringDate", "19000101");
|
|
|
reqBodyObj.put("condition", "1=1");
|
|
|
reqBodyObj.put("secretKey", "f3ac852124d2f991a3f63f2e33dae18b");
|
|
|
|
|
|
- String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/DeleteAssHrtree";
|
|
|
-
|
|
|
- // 计算基本认证编码
|
|
|
- /* String auth = "Basic " + Base64.getEncoder().encodeToString(
|
|
|
- (username + ":" + password).getBytes(StandardCharsets.UTF_8)
|
|
|
- );*/
|
|
|
+ String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/GetAssHrTreeGxb";
|
|
|
|
|
|
// 设置HTTP头部
|
|
|
LinkedHashMap<String, String> header = new LinkedHashMap<>();
|
|
|
// header.put("Authorization", auth);
|
|
|
- header.put("Authorization", baseAuth);
|
|
|
- header.put("Content-Type", "application/json");
|
|
|
+ header.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));
|
|
|
|
|
|
+ // header.put("Authorization", baseAuth);
|
|
|
+ header.put("Content-Type", "application/json");
|
|
|
+ String string = reqBodyObj.toString();
|
|
|
// 发送请求(这里假设HTTPHelper.sendPOST方法支持自定义头部)
|
|
|
try {
|
|
|
SslUtils.ignoreSsl();
|
|
|
- String result = HTTPHelper.sendPOST(postUrl, reqBodyObj.toString(), header, "UTF-8");
|
|
|
+ String result = HTTPHelper.sendPOST(postUrl, string, header, "UTF-8");
|
|
|
System.out.println(result);
|
|
|
+ // String 转 Map
|
|
|
+ JSONObject resultObj = JSONObject.parseObject(result);
|
|
|
+ List<Org> list = (List<Org>) resultObj.get("data");
|
|
|
+ System.out.println(list);
|
|
|
+ orgService.saveBatch(list);
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- public static String getBaseAuth(String userName, String password){
|
|
|
- String auth = userName + ":" + password;
|
|
|
- String base64Auth = cn.hutool.core.codec.Base64.encodeUrlSafe(auth.getBytes());
|
|
|
- return "Basic " + base64Auth;
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- JSONObject reqBodyObj = new JSONObject();
|
|
|
- reqBodyObj.put("app_id", "hcmcloud");
|
|
|
- reqBodyObj.put("table_name", "");
|
|
|
- reqBodyObj.put("stringDate", "20210301");
|
|
|
- reqBodyObj.put("condition", "1=1");
|
|
|
- reqBodyObj.put("secretKey", "f3ac852124d2f991a3f63f2e33dae18b");
|
|
|
- String string = reqBodyObj.toString();
|
|
|
- String username = "po_soap1";
|
|
|
- String password = "z123456789";
|
|
|
- String postUrl = "https://podev.minmetals.com.cn:50001/RESTAdapter/BS_OA/BS_MDM/DeleteAssHrtree";
|
|
|
- String s = PostMethodUtils.sendPost(postUrl, string, username, password);
|
|
|
- System.out.println(s);
|
|
|
- }
|
|
|
-
|
|
|
- public static void main0(String[] args) throws Exception {
|
|
|
- String contentType = "application/pdf";
|
|
|
- JSONObject reqBodyObj = new JSONObject();
|
|
|
- reqBodyObj.put("contentMd5", "contentMD5");
|
|
|
- reqBodyObj.put("contentType", contentType);
|
|
|
- reqBodyObj.put("convertToPDF", true);
|
|
|
- reqBodyObj.put("fileName", "最终版授权声明.docx");
|
|
|
- reqBodyObj.put("fileSize", "123");
|
|
|
- // 请求Body体数据
|
|
|
- String reqBodyData = reqBodyObj.toString();
|
|
|
- // 对请求Body体内的数据计算ContentMD5
|
|
|
- String contentMD5 = "ESignUtils.getBodyContentMD5(reqBodyData";
|
|
|
- System.out.println("请求body数据:" + reqBodyData);
|
|
|
- System.out.println("body的md5值:" + contentMD5);
|
|
|
-
|
|
|
- // 构建待签名字符串
|
|
|
- String method = "POST";
|
|
|
- String accept = "*/*";
|
|
|
- String contentType1 = "application/json; charset=UTF-8";
|
|
|
- String date = "";
|
|
|
- String headers = "";
|
|
|
- String postUrl = "/v3/files/file-upload-url";
|
|
|
-
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
- sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
|
|
|
- .append(contentType1).append("\n").append(date).append("\n");
|
|
|
- if ("".equals(headers)) {
|
|
|
- sb.append(headers).append(postUrl);
|
|
|
- } else {
|
|
|
- sb.append(headers).append("\n").append(postUrl);
|
|
|
- }
|
|
|
-
|
|
|
- // 构建参与请求签名计算的明文
|
|
|
- // String plaintext = sb.toString();
|
|
|
- // 计算请求签名值
|
|
|
- String reqSignature = "ESignUtils.doSignatureBase64(plaintext, appSecret)";
|
|
|
- System.out.println("计算请求签名值:" + reqSignature);
|
|
|
-
|
|
|
- // 获取时间戳(精确到毫秒)
|
|
|
- long timeStamp = System.currentTimeMillis();;
|
|
|
-
|
|
|
- // 构建请求头
|
|
|
- LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
|
|
|
- header.put("X-Tsign-Open-App-Id", "appId");
|
|
|
- header.put("X-Tsign-Open-Auth-Mode", "Signature");
|
|
|
- header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
|
|
|
- header.put("Accept", accept);
|
|
|
- header.put("Content-Type", contentType);
|
|
|
- header.put("X-Tsign-Open-Ca-Signature", reqSignature);
|
|
|
- header.put("Content-MD5", contentMD5);
|
|
|
- System.out.println("header" + header);
|
|
|
-
|
|
|
- String postAllUrl = "https://smlopenapi.esign.cn" + postUrl;
|
|
|
- String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
|
|
|
- JSONObject resultObj = JSONObject.parseObject(result);
|
|
|
- // json转map
|
|
|
- Map<String, Object> resultMap = (Map<String, Object>) JSONObject.toJavaObject(resultObj, Map.class);
|
|
|
- System.out.println("请求返回信息: " + resultObj.toString());
|
|
|
- System.out.println("请求返回信息: " + resultMap.toString());
|
|
|
- }
|
|
|
-
|
|
|
}
|