Httpclient post请求

时间:2023-03-09 10:04:37
Httpclient post请求
 CloseableHttpClient httpclient = HttpClients.createDefault();
String url = "https://ml.yiche.com/test_recognize/rest/v1/face_async_register";
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("charset", HTTP.UTF_8);
httpPost.setHeader("Content-Type",
"application/x-www-form-urlencoded; charset=utf-8"); NameValuePair accessKeyIdPair = new BasicNameValuePair("accessKeyId", "lottery_register");
NameValuePair userIdPair = new BasicNameValuePair("userId", userId);
NameValuePair paramPair = new BasicNameValuePair("userInfo", param);
NameValuePair imagePair = new BasicNameValuePair("image", imageBase64Data);
NameValuePair imgUrlPair = new BasicNameValuePair("imgUrl", "lottery");
List<NameValuePair> nameValuePairList = Lists.newArrayList(accessKeyIdPair
, userIdPair
, paramPair
, imagePair, imgUrlPair); UrlEncodedFormEntity httpEntity = new UrlEncodedFormEntity(nameValuePairList);
httpPost.setEntity(httpEntity);
// 执行post请求.
CloseableHttpResponse response = httpclient.execute(httpPost); if (Objects.nonNull(response) && response.getStatusLine()
.getStatusCode() == HttpStatus.OK.value()) { String reponseLine = EntityUtils.toString(response.getEntity());
if (!StringUtils.isEmpty(reponseLine)) {
JSONObject jsonObject = JSON.parseObject(reponseLine); if (Objects.nonNull(jsonObject)) {
if (jsonObject.getInteger("code") == 0) {
return 1;
}
}
}
} return 0;
}