1.首先导入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2.HttpUtil类
package com.K3demo;
import java.util.Map;
import java.util.Set;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;
public class HttpUtil {
private HttpUtil(){
}
private static final int CONN_TIMEOUT = 30000;
private static final int READ_TIMEOUT = 30000;
private static RestTemplate restTemplate = null;
static{
//设置超时时间
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(CONN_TIMEOUT);
requestFactory.setReadTimeout(READ_TIMEOUT);
restTemplate = new RestTemplate(requestFactory);
}
/**
* 设置header公共参数
*/
private static HttpHeaders initHeader(){
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Accpet-Encoding", "gzip");
headers.add("Content-Encoding", "UTF-8");
headers.add("Content-Type", "application/json; charset=UTF-8");
return headers;
}
/**
* 设置header参数
* @param httpHeaders
* @param headers
*/
private static void setHeaderParam(HttpHeaders httpHeaders,Map<String,Object> headers){
if(!CollectionUtils.isEmpty(headers)){
Set<String> keys = headers.keySet();
for(String key:keys){
httpHeaders.add(key,headers.get(key).toString());
}
}
}
//登录的httpPost
public static ResponseEntity<String> httpPost(String url,String json){
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
//发送请求
return toPost(url,httpHeaders,json);
}
// 保存的httpPost
public static String httpPost(String url,Map<String,Object> header,String json){
//初始化header公共参数
HttpHeaders httpHeaders = initHeader();
setHeaderParam(httpHeaders,header);
//发送请求
return toPost(url,httpHeaders,json).getBody();
}
/**
* 发送请求
* @param json
* @param httpHeaders
* @param url
* @return
*/
private static ResponseEntity<String> toPost(String url,HttpHeaders httpHeaders,String json){
HttpEntity<String> httpEntity = new HttpEntity<>(json,httpHeaders);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST,httpEntity,String.class);
return responseEntity;
}
}
3.测试类
kingdee:
url: http://192.168.0.215/K3Cloud/
login: Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc #登录接口
view: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc #查看表单数据接口
viewList: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc #表单数据查询接口
save: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc #保存接口
batchSave: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.BatchSave.common.kdsvc #批量保存接口
submit: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc #提交接口
audit: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc #审核接口
statusConver: Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.StatusConvert.common.kdsvc #状态转换接口
package com.K3demo;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class K3cloud {
//生成登录的json串
public static String buildLogin(String dbid, String userName, String password, int lang){
Map<String,Object> param = new HashMap<>(4);
param.put("acctID",dbid);
param.put("username",userName);
param.put("password",password);
param.put("lcid",lang);
return JSON.toJSONString(param);
}
//保存的json
public static String buildMaterial(String template,String formid) {
JSONObject basic = JSONObject.parseObject(template, Feature.OrderedField); // 这么写 字符串转json时 顺序不变
//JSONObject basic = JSON.parseObject(template);
JSONObject jsonObject = new JSONObject();
jsonObject.put("formid",formid);
jsonObject.put("data",JSON.toJSONString(basic));
return JSON.toJSONString(jsonObject);
}
//提交和审核的json
public static String buildParam(String formid,String numbers,String flags) {
JSONObject jsonObject = new JSONObject();
JSONObject param = new JSONObject();
if(flags!=null){
String[] arr_flag = flags.split(",");
param.put("InterationFlags",arr_flag);
}
String[] arr_number = numbers.split(",");
param.put("Numbers",arr_number);
jsonObject.put("formid",formid);
jsonObject.put("data",JSON.toJSONString(param));
return JSON.toJSONString(jsonObject);
}
@Scheduled(fixedRate=200000)
public static String login() throws IOException{
// 替换自己的id号 用户名 和 密码
String loginParam = buildLogin("ID号", "用户名", "密码", 2052);
String url = "http://***.***.***.***/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";
ResponseEntity<String> response= HttpUtil.httpPost(url, loginParam);
String login_cookie="";
if(response.getStatusCode()== HttpStatus.OK) {
Set<String> keys=response.getHeaders().keySet();
for(String key:keys){
if (key.equalsIgnoreCase("Set-Cookie")) {
List<String> cookies = response.getHeaders().get(key);
for(String cookie:cookies){
if(cookie.startsWith("kdservice-sessionid")){
login_cookie=cookie;
System.out.println("登录K3成功啦:"+login_cookie);
break;
}
}
}
}
}
return login_cookie;
}
// 需替换api复制下来的json串 参数需自己拼
// 复制api中的表单ID
//替换api相应的url
//替换自己的单据编号
public static void save(String cookie) {
String jsons = " 复制api提供的json串 " ;
String url = "http://***.***.***.***/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc";
String material = buildMaterial(jsons, "表单ID");
Map<String, Object> header = new HashMap<>();
header.put("Cookie",cookie);
String result = HttpUtil.httpPost(url, header, material);
JSONObject jsonObject = JSON.parseObject(result);
System.out.println("保存结果:"+jsonObject);
}
public static void submit(String cookie) {
String url = "http://***.***.***.***/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc";
String material = buildParam("表单ID", "单据编号", null) ;
Map<String, Object> header = new HashMap<>();
header.put("Cookie",cookie);
String result = HttpUtil.httpPost(url, header, material);
JSONObject jsonObject = JSON.parseObject(result);
System.out.println("提交结果:"+jsonObject);
}
public static void audit(String cookie) {
String url = "http://***.***.***.***/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc";
String material = buildParam("表单ID", "单据编号", null) ;
Map<String, Object> header = new HashMap<>();
header.put("Cookie",cookie);
String result = HttpUtil.httpPost(url, header, material);
JSONObject jsonObject = JSON.parseObject(result);
System.out.println("审核结果:"+jsonObject);
}
public static void main(String[] args) throws IOException {
String cookie = login();
save(cookie);
submit(cookie);
audit(cookie);
}
}
参考原文: https://blog.csdn.net/u013890624/article/details/86022898
亲测可用