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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM