封装HttpClient工具类

添加依赖:

     <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>

代码:


public class HttpUtil {


    /**
     * get请求
     * @param url
     * @param param
     * @return
     */
    public static String doGet(String url, Map<String, Object> param) {
        //创建默认的httpclient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //响应对象
        CloseableHttpResponse response = null;
        //返回结果
        String resultStr = "";
        try {
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, String.valueOf(param.get(key)));
                }
            }
            URI uri = builder.build();
            HttpGet httpGet = new HttpGet(uri);
            response = httpClient.execute(httpGet);

            if (response!=null && response.getStatusLine().getStatusCode() == 200){
                HttpEntity httpEntity = response.getEntity();
                resultStr = EntityUtils.toString(httpEntity,"UTF-8");
            }

        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return resultStr;
    }


    /**
     * Post请求
     * @param url
     * @param param
     * @return
     */
    public static String doPost(String url, Map<String, Object> param, Map<String, Object> headData) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";

        try {
        HttpPost httpPost = new HttpPost(url);
            if (param != null) {
                List<NameValuePair> formParams = new ArrayList<NameValuePair>();
                for (String key : param.keySet()) {
                    formParams.add(new BasicNameValuePair(key, String.valueOf(param.get(key))));
                }
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams,"UTF-8");
                httpPost.setEntity(entity);
            }
            //携带head数据
            if (headData != null && headData.size() > 0) {
                for (String key : headData.keySet()) {
                    httpPost.setHeader(key, (String) headData.get(key));
                }
            }

            //执行post请求
            response = httpClient.execute(httpPost);

            if (response!=null && response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(),"UTF-8");
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return resultString;
    }
}
发布了102 篇原创文章 · 获赞 49 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/SoWhatWorld/article/details/104807595