Files
jijiaqi/src/main/java/com/example/sso/util/YunUtil.java
2025-09-23 16:59:06 +08:00

244 lines
7.5 KiB
Java

package com.example.sso.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class YunUtil {
public static String token(){
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建 POST 请求对象
HttpPost httpPost = new HttpPost("https://openapi.yunxuetang.cn/token?appId=ml8bfwvx02g&appSecret=MTRjZTBiYTU4YWQ1");
String responseBody = null;
try {
// 设置请求头
// 执行请求,获取响应对象
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
// 从响应对象中获取响应实体
HttpEntity responseEntity = response.getEntity();
// 处理响应数据
responseBody = EntityUtils.toString(responseEntity);
System.out.println(responseBody);
} finally {
// 关闭响应对象
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭 HttpClient
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
JSONObject jsonObject = JSON.parseObject(responseBody);
String string = jsonObject.getString("accessToken");
return string;
}
public static String tokens(){
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建 POST 请求对象
HttpPost httpPost = new HttpPost("https://openapi.yunxuetang.cn/token?appId=ml8bfwvx02g&appSecret=MTRjZTBiYTU4YWQ1");
String responseBody = null;
try {
// 设置请求头
// 执行请求,获取响应对象
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
// 从响应对象中获取响应实体
HttpEntity responseEntity = response.getEntity();
// 处理响应数据
responseBody = EntityUtils.toString(responseEntity);
System.out.println(responseBody);
} finally {
// 关闭响应对象
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭 HttpClient
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
JSONObject jsonObject = JSON.parseObject(responseBody);
String string = jsonObject.getString("accessToken");
return string;
}
public static String tongbu(String jsonBody){
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建 POST 请求对象
HttpPost httpPost = new HttpPost("https://openapi.yunxuetang.cn/v1/udp/public/users/sync/batch");
String responseBody = null;
try {
String token = token();
// 设置请求头
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
httpPost.setHeader("Authorization", token);
StringEntity entity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行请求,获取响应对象
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
// 从响应对象中获取响应实体
HttpEntity responseEntity = response.getEntity();
// 处理响应数据
responseBody = EntityUtils.toString(responseEntity);
System.out.println(responseBody);
} finally {
// 关闭响应对象
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭 HttpClient
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseBody;
}
public static String bumen(String jsonBody){
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建 POST 请求对象
HttpPost httpPost = new HttpPost("https://openapi.yunxuetang.cn/v1/udp/public/depts/sync");
String responseBody = null;
try {
String token = token();
// 设置请求头
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
httpPost.setHeader("Authorization", token);
StringEntity entity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行请求,获取响应对象
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
// 从响应对象中获取响应实体
HttpEntity responseEntity = response.getEntity();
// 处理响应数据
responseBody = EntityUtils.toString(responseEntity);
System.out.println(responseBody);
} finally {
// 关闭响应对象
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭 HttpClient
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseBody;
}
public static String alluser(String jsonBody){
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建 POST 请求对象
HttpPost httpPost = new HttpPost("https://openapi.yunxuetang.cn/v1/rpt2open/public/o2o/project/student/sync/all");
String responseBody = null;
try {
String token = tokens();
// 设置请求头
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
httpPost.setHeader("Authorization", token);
StringEntity entity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行请求,获取响应对象
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
// 从响应对象中获取响应实体
HttpEntity responseEntity = response.getEntity();
// 处理响应数据
responseBody = EntityUtils.toString(responseEntity);
System.out.println(responseBody);
} finally {
// 关闭响应对象
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭 HttpClient
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseBody;
}
}