java发送https请求:同时附带auth 验证
)
public static String doPost(String url, Map<String, String> map, String charset, String username, String password) {
org.apache.http.client.HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
System.out.println("要发送的数据" + JSONUtils.toJSONString(map));
String json = JSONUtils.toJSONString(map);
StringEntity stringEntity = new StringEntity(json);
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
String value2 = "Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes());
//设置头
System.out.println("要value2数据 :" + value2);
httpPost.addHeader("Authorization", value2);
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}