String encode = "utf-8";
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPut httpput = new HttpPut(url);
// 设置header
httpput.setHeader("Content-type", "application/json");
String authUser = "admin:admin";
String base64Credentials = new String(Base64.encodeBase64(authUser.getBytes()));
httpput.setHeader("Authorization", "Basic " + base64Credentials);
// 组织请求参数
StringEntity stringEntity = new StringEntity(stringJson, encode);
httpput.setEntity(stringEntity);
String content = null;
CloseableHttpResponse httpResponse = null;
try {
// 响应信息
httpResponse = closeableHttpClient.execute(httpput);
HttpEntity entity = httpResponse.getEntity();
content = EntityUtils.toString(entity, encode);
System.out.println(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
closeableHttpClient.close(); // 关闭连接、释放资源
} catch (IOException e) {
e.printStackTrace();
}
httpdelete
HttpClient client = new DefaultHttpClient();
HttpDelete delete = new HttpDelete(url);
HttpResponse httpResponse;
try {
delete.setHeader("Cookie",
"grafana_user=admin; grafana_remember=×××××6; grafana_sess=5c323d912ccb3773;");
// delete.setHeader("authorization",
// "Bearer 7cebf17a9c8b6f00cb4d2b4bfc98a3459361283d1e73801ab1c1466d8fd0a96bf6");
httpResponse = client.execute(delete);
int statusCode = httpResponse.getStatusLine().getStatusCode();
System.out.println(statusCode);
网友评论