String url ="接口ip";
Map<String, String> prarms = new HashMap<>();
prarms.put("key","value");
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //获取浏览器信息
HttpPost httpPost = new HttpPost(url);
List<BasicNameValuePair> list = new ArrayList<>();
for (String key : prarms.keySet()) {
list.add(new BasicNameValuePair(key, String.valueOf(prarms.get(key))));
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "utf-8");
httpPost.setEntity(urlEncodedFormEntity);
httpPost.addHeader("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("username:password").getBytes()));
HttpResponse response = httpClient.execute(httpPost);
StatusLine statusLine = response.getStatusLine(); //获取请求对象中的响应行对象
int responseCode = statusLine.getStatusCode();
if (responseCode == 200) {
//获取响应信息
HttpEntity entity = response.getEntity();
InputStream input = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
String str1 = br.readLine();
System.out.println("服务器的响应是:" + str1);
br.close();
input.close();
} else {
System.out.println("响应失败");
}
}
HTTP post请求 对方接口参数为map 带Basic Auth认证
最新推荐文章于 2024-04-28 11:34:51 发布