• HttpClientUitl工具类


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

    }
  • 相关阅读:
    MessageDigest简介
    深入入门正则表达式(java)
    JAVA 正则 Pattern 和 Matcher
    理解Servlet过滤器 (javax.servlet.Filter)
    Java 之 I/O 系列 01 ——基础
    finally块中的代码一定会执行吗?
    wait(), notify(),sleep详解
    Java 多线程——基础知识
    集合迭代时对集合进行修改抛ConcurrentModificationException 原因 以及解决方案
    深入理解ServletRequest与ServletResponse
  • 原文地址:https://www.cnblogs.com/h-c-g/p/9978849.html
Copyright © 2020-2023  润新知