所用依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
Get请求方式举例
代码:
public class Test {
public static void main(String[] args) {
System.out.println(doGet("http://www.baidu.com"));
}
private static String doGet(String apiUrl) {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
client = HttpClients.createDefault();
HttpGet get = new HttpGet(apiUrl);
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(3000)
.setSocketTimeout(20000).build();
get.setConfig(config);
response = client.execute(get);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (client != null) {
client.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
结果:
<!DOCTYPE html><!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=
POST请求方式举例
例1:获取token
代码
public class Test {
public static void main(String[] args) {
System.out.println("token:" + doPost("接口地址", "root", "123456", "default"));
}
private static String doPost(String apiUrl, String username, String password, String tenantUrl) {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
String idToken = null;
try {
client = HttpClients.createDefault();
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Content-Type", "application/json;charset=utf-8");
post.setHeader("Accept", "application/json;charset=utf-8");
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(3000).setSocketTimeout(20000).build();
post.setConfig(config);
JSONObject json = new JSONObject();
json.put("username", username);
json.put("password", DigestUtils.md5Hex(password));
json.put("tenantUrl", tenantUrl);
post.setEntity(new StringEntity(json.toString(), "UTF-8"));
response = client.execute(post);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
if (result != null && !"".equals(result.trim())) {
JSONObject rjo = JSONObject.parseObject(result, Feature.OrderedField);
if (rjo != null && rjo.containsKey("retCode")) {
int retCode = rjo.getInteger("retCode");
if (retCode == 0 && rjo.containsKey("data")) {
JSONObject data = rjo.getJSONObject("data");
if (null != data && data.containsKey("idToken")) {
idToken = data.getString("idToken");
}
}
}
}
} catch (SocketTimeoutException e) {
e.printStackTrace();
throw new RuntimeException("获取token出现连接/超时异常");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("获取token时执行内部代码时出现异常");
} finally {
try {
if (client != null) {
client.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return idToken;
}
}
结果
token:853b7c87-c16f-4e3b-96c4-9701fe3448ab
接口文档
  
Postman访问示例
 
例2:获取知识图谱信息
代码
public class Test {
public static void main(String[] args) {
System.out.println("图谱结果:" + doPost("接口地址", "实体id", "token令牌", "图谱实例数量"));
}
private static String doPost(String apiUrl, String instanceId, String idToken, Integer number) {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
String result = null;
try {
client = HttpClients.createDefault();
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Content-Type", "application/json;charset=utf-8");
post.setHeader("Accept", "application/json;charset=utf-8");
post.setHeader("Authorization", "Bearer " + idToken);
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(3000).setSocketTimeout(20000).build();
post.setConfig(config);
JSONObject json = new JSONObject();
json.put("pageNum", 0);
json.put("pageSize", number);
JSONObject data = new JSONObject();
String[] status = {"on"};
data.put("status", status);
String[] instanceIdSet = {instanceId};
data.put("instanceIdSet", instanceIdSet);
String[] checked = {"on"};
data.put("checked", checked);
json.put("data", data);
post.setEntity(new StringEntity(json.toString(), "UTF-8"));
response = client.execute(post);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (SocketTimeoutException e) {
e.printStackTrace();
throw new RuntimeException("调用获取图谱接口出现连接/超时异常");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("调用获取图谱接口执行内部代码时出现异常");
} finally {
try {
if (client != null) {
client.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
接口文档
   
Postman访问示例
 
例3:调用ES高级接口获取实体信息
代码
public class HTTPURLConnectionTest {
public static void main(String[] args) {
System.out.println("图谱结果:" + doPost("接口地址", "图谱id", "实体id", "token令牌"));
}
private static String doPost(String apiUrl, String ontologyId, String instanceId, String idToken) {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
String result = null;
try {
client = HttpClients.createDefault();
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Content-Type", "application/json;charset=utf-8");
post.setHeader("Accept", "application/json;charset=utf-8");
post.setHeader("Authorization", "Bearer " + idToken);
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(3000).setSocketTimeout(20000).build();
post.setConfig(config);
JSONObject json = new JSONObject();
json.put("pageNum", 0);
json.put("pageSize", 1);
JSONObject data = new JSONObject();
data.put("ontologyId", ontologyId);
data.put("needProperties", true);
JSONArray expresses = new JSONArray();
JSONObject arr = new JSONObject();
arr.put("sign", "=");
arr.put("value", instanceId);
arr.put("key", "id");
arr.put("dataType", "String");
expresses.add(arr);
data.put("expresses", expresses);
json.put("data", data);
post.setEntity(new StringEntity(json.toString(), "UTF-8"));
response = client.execute(post);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (SocketTimeoutException e) {
e.printStackTrace();
throw new RuntimeException("调用获取图谱接口出现连接/超时异常");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("调用获取图谱接口执行内部代码时出现异常");
} finally {
try {
if (client != null) {
client.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
接口文档
   
Postman访问示例
 
拓展
HttpURLConnection的用法
|