开发中 和第三方联调的时候 需要用到调用第三方接口,在此以作笔记,仅供学习参考使用
1. 将需要的传的参数转JSON
String params = JSONObject.toJSONString(restVerb);
log.info("请求接口地址==============="+OPEN_URL);
result = doPost(OPEN_URL, params, token);
if (isJsonObject(result)){
JSONObject jsonResult = JSONObject.parseObject(result);
Object codeObj = jsonResult.get("result");
if(codeObj != null && !StringUtils.isEmpty(codeObj.toString())){
code = codeObj.toString();
}
}
log.info("请求之后返回的结果========************======="+result);
dopost 方法 (PSOT请求方式)
public String doPost(String url, String params, String token) throws Exception {
log.info("invoke url:"+url+" \nparams:"+params + " \ntoken:"+token);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
if(!"".equals(token)) {
httpPost.setHeader("Authorization", token);
}
String charSet = "UTF-8";
StringEntity entity = new StringEntity(params, charSet);
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
int state = 999;
String jsonString = "";
try {
response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
} else{
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
log.info("jsonString:"+jsonString + " 请求返回状态:" + state + "("+url+")");
}
} catch(Exception e) {
e.printStackTrace();
log.info("url:"+url+" params:"+params+" Exception:"+e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonString;
}
DELETE请求方式(写法和post差不多),如下:
public String doPostBack(String url, String params, String token) throws Exception {
log.info("invoke url:"+url+" \nparams:"+params + " \ntoken:"+token);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpDeleteWithBody httpDel = new HttpDeleteWithBody(url);
httpDel.setHeader("Content-type", "application/json; charset=utf-8");
String jsonString = "";
int state = 999;
try {
if (null != params) {
StringEntity entity = new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_JSON);
httpDel.setEntity(entity);
}
CloseableHttpResponse response = httpclient.execute(httpDel);
StatusLine status = response.getStatusLine();
state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
} else{
HttpEntity responseEntity = response.getEntity();
jsonString = EntityUtils.toString(responseEntity);
log.info("jsonString:"+jsonString + " 请求返回状态:" + state + "("+url+")");
}
} catch (IOException e) {
log.info("delete请求提交失败:" + url, e);
} finally {
httpDel.releaseConnection();
}
return jsonString;
}
以上在学习工作中的记录,分享一下,给朋友们提供参考,有不到之处,请多指教
|