2019-09-15 16:21:41 版本 : WCP4.3.0版API调用DEMO
作者: 系统管理员 于 2019年09月15日 发布在分类 / 资讯新闻 / WCP知识管理 / WCP免费版 下,并于 2019年09月15日 编辑
 历史版本

备注 修改日期 修改人
格式调整 2021-07-08 10:57:38[当前版本] 系统管理员
创建版本 2019-09-15 16:21:41 系统管理员

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;


public class RestFulApiTester {

	public static String baseUrl = "http://127.0.0.1:8080/wcp/";
	public static String secret = "FC7E18DA4DCA06FFB087AD65591D49B0";
	public static String testOrgId = null;
	public static String testUserId = null;

	public static void checkState(JSONObject json) {
		String state = json.get("STATE").toString();
		if (state.equals("1")) {
			throw new RuntimeException(json.getString("MESSAGE"));
		}
	}

	public static void test1() {
		System.out.println("组织机构查询");
		Map<String, String> data = new HashMap<>();
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/get/organization.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test2() {
		System.out.println("创建组织机构");
		Map<String, String> data = new HashMap<>();
		data.put("secret", secret);
		data.put("sort", "1");
		data.put("name", "testOrg");
		JSONObject obj = httpPost(baseUrl + "outer/post/organization.do", data);
		checkState(obj);
		System.out.println(obj.getString("ID"));
		testOrgId = obj.getString("ID");
	}

	public static void test3() {
		System.out.println("更新组织机构");
		Map<String, String> data = new HashMap<>();
		data.put("secret", secret);
		data.put("id", testOrgId);
		data.put("comments", "update");
		JSONObject obj = httpPost(baseUrl + "outer/put/organization.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test4() {
		System.out.println("删除组织机构");
		Map<String, String> data = new HashMap<>();
		data.put("secret", secret);
		data.put("id", testOrgId);
		JSONObject obj = httpPost(baseUrl + "outer/delete/organization.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test5() {
		System.out.println("用户查询");
		Map<String, String> data = new HashMap<>();
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/get/user.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test6() {
		System.out.println("创建用户");
		Map<String, String> data = new HashMap<>();
		data.put("name", secret);
		data.put("loginname", secret);
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/post/user.do", data);
		checkState(obj);
		testUserId = obj.getString("ID");
		System.out.println(obj);
	}

	public static void test7() {
		System.out.println("更新用户");
		Map<String, String> data = new HashMap<>();
		data.put("id", testUserId);
		data.put("comments", "update");
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/put/user.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test8() {
		System.out.println("删除用户");
		Map<String, String> data = new HashMap<>();
		data.put("id", testUserId);
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/delete/user.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test9() {
		System.out.println("分类查询");
		Map<String, String> data = new HashMap<>();
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/get/category.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test10() {
		System.out.println("知识查询");
		Map<String, String> data = new HashMap<>();
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/get/know.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test11() {
		System.out.println("问答查询");
		Map<String, String> data = new HashMap<>();
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/get/fqa.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test12() {
		System.out.println("全文检索");
		Map<String, String> data = new HashMap<>();
		data.put("word", "知识");
		data.put("domtype", "all");
		data.put("secret", secret);
		JSONObject obj = httpPost(baseUrl + "outer/search.do", data);
		checkState(obj);
		System.out.println(obj);
	}

	public static void test13() {
		System.out.println("免密登陸");
		Map<String, String> data = new HashMap<>();
		data.put("loginname", "sysadmin");
		data.put("secret", secret);
		System.out.println("通过登录名" + "sysadmin" + "换取注册码...");
		JSONObject obj = httpPost(baseUrl + "outer/regist/login.do", data);
		String loginCode = obj.get("CERTIFICATE").toString();
		System.out.println("注册码为:" + loginCode);
		// -----------------------------------------------------------------------------------------
		Map<String, String> data2 = new HashMap<>();
		data2.put("certificate", loginCode);
		data2.put("secret", secret);
		System.out.println("通过注册码" + loginCode + "换取登录名 ...");
		JSONObject objs = httpPost(baseUrl + "outer/get/login.do", data2);
		System.out.println("换取的登录名为:" + objs.getString("LOGINNAME"));
	}

	public static JSONObject httpPost(String url, Map<String, String> data) {
		long startTime = new Date().getTime();
		HttpPost httpPost = new HttpPost(url);
		CloseableHttpResponse response = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
		httpPost.setConfig(requestConfig);
		// httpPost.addHeader("Content-Type", "application/json");
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			for (String key : data.keySet()) {
				params.add(new BasicNameValuePair(key, data.get(key)));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));
			response = httpClient.execute(httpPost, new BasicHttpContext());
			if (response.getStatusLine().getStatusCode() != 200) {
				System.out.println(
						"request url failed, http code=" + response.getStatusLine().getStatusCode() + ", url=" + url);
				return null;
			}
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String resultStr = EntityUtils.toString(entity, "utf-8");
				JSONObject result = new JSONObject(resultStr);
				return result;
			}
		} catch (IOException e) {
			if (url.indexOf("info/version") <= 0) {
				System.out.println("request url=" + url + ", exception, msg=" + e.getMessage());
			}
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				System.out.println("httpPost:" + e + e.getMessage());
			}
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					System.out.println("httpPost:" + e + e.getMessage());
				}
			}
			long endTime = new Date().getTime();
			System.out.println("httpPost-runtime:" + (endTime - startTime));
		}
		return null;
	}
}

历史版本-目录  [回到顶端]
    wcp知识库系统-京ICP备15024440号-1 -V 5.1.9 -wcp