`

// 发送请求HTTP-POST请求 url:请求地址; entity:json格式请求参数

public static String post(String url, String entity) {

try {

String username="user";// 账户

String password="1234";// 密码

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost httpPost = new HttpPost(url);

httpPost.addHeader("Authorization", "Basic " + java.util.Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));

StringEntity se = new StringEntity(entity, "UTF-8");

se.setContentType("application/json");

httpPost.setEntity(se);

CloseableHttpResponse response = httpClient.execute(httpPost);

HttpEntity entity1 = response.getEntity();

String resStr = null;

if (entity1 != null) {

resStr = EntityUtils.toString(entity1, "UTF-8");

}

httpClient.close();

response.close();

return resStr;

} catch (Exception e) {

e.printStackTrace();

}

return "";

}

`

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐