Cloud系统集成 Java完整调用示例【分享】
金蝶云社区-云社区用户4u731234
云社区用户4u731234
2人赞赏了该文章 1.1万次浏览 未经作者许可,禁止转载编辑于2020年02月13日 08:41:06

上图,上真相:

/*
Java Cloud 系统集成 登陆并查看用户表记录
by wanghl 2014-12-25
测试环境 Cloud5.0

Copy 如下代码即可在Java环境中运行

注意:需要先定义【动态表单服务列表】的业务对象“用户”的View 查看接口
*/
package JavaInvoke;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpInvoker {

public static final String POST_URL_Login = "http://localhost/K3CloudServiceInterface/json/syncreply/Auth ";
public static final String POST_URL_Invoke = "http://localhost/K3CloudServiceInterface/json/syncreply/SEC_User_View ";
public static void readContentFromPost() throws IOException {

URL postUrl = new URL(POST_URL_Login);
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
if(!connection.getDoOutput())
{
connection.setDoOutput(true);
}
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
//登陆验证字串
String content = "{\"provider\":\"credentials\",\"UserName\":\"Administrator\",\"Password\":\"888888\",\"RememberMe\":false\"PasswordIsEncrypted\":false}";
out.writeBytes(content);
out.flush();
out.close();

//获取Cookie
String key=null;
String cookieVal = null;
for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++ ) {
if (key.equalsIgnoreCase("Set-Cookie")) {
cookieVal = connection.getHeaderField(i);
break;
}
}

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
System.out.println(" ============================= ");
System.out.println(" Contents of post request ");
System.out.println(" ============================= ");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println(" ============================= ");
System.out.println(" Contents of post request ends ");
System.out.println(" ============================= ");
reader.close();

connection.disconnect();

URL postUrlInvoke = new URL(POST_URL_Invoke);
HttpURLConnection connectionInvoke = (HttpURLConnection)postUrlInvoke.openConnection();
connectionInvoke.setRequestProperty("Cookie", cookieVal);
if(!connectionInvoke.getDoOutput())
{
connectionInvoke.setDoOutput(true);
}
connectionInvoke.setRequestMethod("POST");
connectionInvoke.setUseCaches(false);
connectionInvoke.setInstanceFollowRedirects(true);
connectionInvoke.setRequestProperty("Content-Type", "application/json");
out = new DataOutputStream(connectionInvoke.getOutputStream());
//用户新增字串 启用此字串会向Cloud SEC_User用户 增加一条登陆账号为java的记录
content = "{\"Creator\":\"Java\",\"NeedUpDateFields\":[\"\"],\"Model\":{\"FUserID\":0,\"FUserAccount\":\"Java\",\"FName\":\"Java\"}}";
//用户查询字串 Id 16394 为Cloud Administrator管理员主键Id
content = "{\"CreateOrgId\":0,\"Number\":\"\",\"Id\":\"16394\"}";
out.writeBytes(content);
out.flush();
out.close();

reader = new BufferedReader(new InputStreamReader(connectionInvoke.getInputStream()));
System.out.println(" ============================= ");
System.out.println(" Contents of post request ");
System.out.println(" ============================= ");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println(" ============================= ");
System.out.println(" Contents of post request ends ");
System.out.println(" ============================= ");
reader.close();

connectionInvoke.disconnect();
}

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
readContentFromPost();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}