【亚伟-苍穹系列】之JAVA调用苍穹获取AppToken和login接口示例原创
8人赞赏了该文章
1,894次浏览
编辑于2022年05月06日 18:10:22
package kd.bos.formplugin; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Map; 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.HttpClients; import json.JSON; import json.JSONObject; import kd.bos.api.ApiRequestContext; import kd.bos.dc.api.model.Account; import kd.bos.dc.utils.AccountUtils; import kd.bos.lang.Lang; import kd.bos.login.LoginClientEnum; import kd.bos.login.user.LoginUserService; import kd.bos.login.utils.SessionUtils; import kd.bos.login.utils.ThirdAPIAPPUtils; import kd.bos.util.NetAddressUtils; public class HttpDispathcer { public static String login() { JSONObject requestData = new JSONObject(); requestData.put("appId", "master_data_pull"); requestData.put("appSecuret", "123456"); requestData.put("tenantid", "next"); requestData.put("accountId", "759401979301596160"); requestData.put("language", "zh_CN"); String json = post(requestData,"http://localhost:8080/ierp/api/getAppToken.do"); System.out.println(json); JSONObject resultObj = (JSONObject) JSON.parse(json); JSONObject data = resultObj.getJSONObject("data"); String apptoken = data.getString("app_token"); String tenantid = "next"; String user = "1355555555"; String accountId = "759401979301596160"; String usertype = "Mobile"; JSONObject loginParam = new JSONObject(); loginParam.put("apptoken", apptoken); loginParam.put("tenantid", tenantid); loginParam.put("user", user); loginParam.put("accountId", accountId); loginParam.put("usertype", usertype); String result = post(loginParam,"http://localhost:8080/ierp/api/login.do"); System.out.println(result); return result; } public static String post(JSONObject json, String url){ String result = ""; HttpPost post = new HttpPost(url); try{ CloseableHttpClient httpClient = HttpClients.createDefault(); post.setHeader("Content-Type","application/json;charset=utf-8"); post.addHeader("Authorization", "Basic YWRtaW46"); StringEntity postingString = new StringEntity(json.toString(),"utf-8"); post.setEntity(postingString); HttpResponse response = httpClient.execute(post); InputStream in = response.getEntity().getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8")); StringBuilder strber= new StringBuilder(); String line = null; while((line = br.readLine())!=null){ strber.append(line+'\n'); } br.close(); in.close(); result = strber.toString(); if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){ result = "服务器异常"; } } catch (Exception e){ System.out.println("请求异常"); throw new RuntimeException(e); } finally{ post.abort(); } return result; } }
推荐阅读