/**
* httpclient post请求
* @param url 请求地址
* @param jsonStr 请求体
* @param tenantToken 请求token
* @return
*/
public String doPost(String url,String jsonStr,String tenantToken){
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(HTTP.CONTENT_TYPE,"application/json; charset=utf-8");
if(StringUtils.isNotEmpty(tenantToken)){
httpPost.setHeader("Authorization",tenantToken);
}
StringEntity entity = new StringEntity(jsonStr,"utf-8");
httpPost.setEntity(entity);
HttpResponse executeResp = null;
try{
executeResp = httpClient.execute(httpPost);
if (executeResp.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
String resultJsonStr = EntityUtils.toString(executeResp.getEntity());
return resultJsonStr;
}
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
/**
* httpclient get请求
* @param url 请求地址
* @param tenantToken 请求token
* @return
*/
public String doGet(String url,String tenantToken) {
try {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet=new HttpGet(url);
httpGet.setHeader(HTTP.CONTENT_TYPE,"application/json; charset=utf-8");
if(StringUtils.isNotEmpty(tenantToken)){
httpGet.setHeader("Authorization",tenantToken);
}
HttpResponse result = httpClient.execute(httpGet);
if (result.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
String resultJsonStr = EntityUtils.toString(result.getEntity());
return resultJsonStr;
}
} catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
|