前言
通过iObjects Java调用iServer的rest api删除iServer上已发布的工作空间
一、创建PUT请求方式实例
//创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建PUT请求方式实例
HttpPut httpPut = new HttpPut(deleteUrl + "?token=" + token);
注:
1、此处deleteUrl 固定为http://ip:端口/iserver/manager/workspaces.rjson
2、token为iServer中的令牌
二、构造消息实体
1.构造
String params = "{\"workspaceConnectionInfo\":" + "\"" + urlToRead.replaceAll("\\\\", "/") + "\"" + "}";
JSONObject jsonObject = JSONObject.parseObject(params);
System.out.println(params);
//构建消息实体
StringEntity entity = new StringEntity(jsonObject.toString(), Charset.forName("UTF-8"));
注:此处urlToRead为工作空间在本地的绝对路径
2.设置编码格式并把消息实体塞进去
// 设置编码格式
entity.setContentEncoding("UTF-8");
// 发送Json格式的数据请求
entity.setContentType("application/json");
// 把请求消息实体塞进去
httpPut.setEntity(entity);
三、执行http的put请求
// 执行http的put请求
CloseableHttpResponse httpResponse;
String result = null;
try {
//发送请求
httpResponse = httpClient.execute(httpPut);
result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}