public class HttpClient {

   private CloseableHttpClient httpClient;

   public HttpClient() {
this.httpClient = HttpClients.createDefault();
} /**
* 带参数的get请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public HttpResult doGet(String url, Map<String, Object> map) throws Exception {
URIBuilder uriBuilder = new URIBuilder(url);
if (map != null) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
HttpGet httpGet = new HttpGet(uriBuilder.build());
CloseableHttpResponse response = this.httpClient.execute(httpGet);
HttpResult httpResult = null;
if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
}
return httpResult;
} /**
* 不带参数的get请求
*
* @param url
* @return
* @throws Exception
*/
public HttpResult doGet(String url) throws Exception {
HttpResult httpResult = this.doGet(url, null);
return httpResult;
} /**
* 带参数的post请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public HttpResult doPost(String url, Map<String, Object> map) throws Exception {
HttpPost httpPost = new HttpPost(url);
if (map != null) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPost.setEntity(formEntity);
}
CloseableHttpResponse response = this.httpClient.execute(httpPost);
HttpResult httpResult = null;
if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
}
return httpResult;
} /**
* 不带参数的post请求
*
* @param url
* @return
* @throws Exception
*/
public HttpResult doPost(String url) throws Exception {
HttpResult httpResult = this.doPost(url, null);
return httpResult;
} /**
* 带参数的Put请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public HttpResult doPut(String url, Map<String, Object> map) throws Exception {
HttpPut httpPut = new HttpPut(url);
if (map != null) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPut.setEntity(formEntity);
}
CloseableHttpResponse response = this.httpClient.execute(httpPut);
HttpResult httpResult = null;
if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
}
return httpResult;
} /**
* 带参数的Delete请求
*
* @param url
* @param map
* @return
* @throws Exception
*/
public HttpResult doDelete(String url, Map<String, Object> map) throws Exception {
URIBuilder uriBuilder = new URIBuilder(url);
if (map != null) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
}
}
HttpDelete httpDelete = new HttpDelete(uriBuilder.build());
CloseableHttpResponse response = this.httpClient.execute(httpDelete);
HttpResult httpResult = null;
if (response.getEntity() != null) {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} else {
httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
}
return httpResult;
} /**
* post请求(用于请求json格式的参数)
* @param url
* @param params
* @return
*/
public static String doPostJson(String url, String params) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);// 创建httpPost
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
String charSet = "UTF-8";
StringEntity entity = new StringEntity(params, charSet);
httpPost.setEntity(entity);
CloseableHttpResponse response = null; try { response = httpclient.execute(httpPost);
StatusLine status = response.getStatusLine();
int state = status.getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity responseEntity = response.getEntity();
String jsonString = EntityUtils.toString(responseEntity);
return jsonString;
}
else{
logger.error("请求返回:"+state+"("+url+")");
}
}
finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} }

最新文章

  1. python 筛选股票
  2. 浅析 IDE跟编译器
  3. 【C语言】C语言运算符
  4. 如何拿到半数面试公司Offer——我的Python求职之路
  5. jsp_包含指令
  6. codeforces 424D
  7. 【转】linux下a.out &gt;outfile 2&gt;&amp;1重定向问题
  8. InvoiceCancelSendApAction
  9. AjaxPro使用说明
  10. BZOJ2213: [Poi2011]Difference
  11. SpringBoot+AOP整合
  12. MySQL将一张表的某些列数据,复制到另外一张表,并且修改某些内容
  13. Python: re.sub()第二个参数
  14. RestFul风格接口示例
  15. java struts2入门学习---自定义类型转换
  16. LeetCode——Integer to Roman
  17. TFIDF练习
  18. CentOS7.3部署镜像仓库Harbor
  19. 无源码情况下直接修改jar里内容思路
  20. flask请求和应用上下文

热门文章

  1. Delphi获取句柄
  2. NX二次开发-UFUN创建固定的基准平面UF_MODL_create_fixed_dplane
  3. NApache+JBOSS架构方案
  4. 牛客多校第六场 D move 枚举/机智题
  5. code rain???
  6. 20140319 const sizeof define 编译时分配内存
  7. 消息中间件kafka学习记录
  8. mac下xampp+vscode进行php程序调试
  9. ransformResourcesWithMergeJavaResForDebug问题
  10. curl 基础