httpClient工具类

httpclient工具类

本人封装的,常用的get、post表单提交、postJson提交都已做了封装,暂时没有加上请求超时时间

public class HttpClientUtil {
    /**
     * 发送get请求
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws URISyntaxException
     */
    public static final String doGet(String url) throws ClientProtocolException, IOException, URISyntaxException{
        return doGet(url,null);
    }
    /**
     * 发送get请求
     * @param url
     * @param param
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws URISyntaxException
     */
    public static final String doGet(String url,Map<String,String> param) throws ClientProtocolException, IOException, URISyntaxException{
        return doGet(url,null,null);
    }

    /**
     * 发送get请求
     * @param url
     * @param param
     * @param headers
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     * @throws URISyntaxException
     */
    public static final String doGet(String url,Map<String,String> param,Map<String,Object> headers) throws ClientProtocolException, IOException, URISyntaxException{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String resultString =null;
        CloseableHttpResponse response =null;
        try{
            URIBuilder uriBuilder = new URIBuilder(url);
            if(param!=null){
                for(Map.Entry<String, String> entry:param.entrySet()){
                    uriBuilder.addParameter(entry.getKey(), entry.getValue());
                }
            }
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            setHeaders(httpGet, headers);
            response=httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                resultString=EntityUtils.toString(response.getEntity(),"UTF-8");
            }
        }
        finally{
            if(response!=null){
                response.close();
            }
            httpClient.close();
        }
        return resultString;
    }
    /**
     * 发送POST请求
     * @param url
     * @param json
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static final String doPostJson(String url,String json) throws ClientProtocolException, IOException{
        return doPostJson(url, json,null);
    }


    /**
     * 发送POST请求
     * @param url
     * @param json
     * @param headers
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static final String doPostJson(String url,String json,Map<String,Object> headers) throws ClientProtocolException, IOException{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String resultString =null;
        CloseableHttpResponse response =null;
        try{
            HttpPost httpPost = new HttpPost(url);
            StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(stringEntity);
            httpPost.setHeader("Content-Type", "application/json");
            setHeaders(httpPost, headers);
            response=httpClient.execute(httpPost);
            if(response.getStatusLine().getStatusCode()==200){
                resultString=EntityUtils.toString(response.getEntity(),"UTF-8");
            }
        }
        finally{
            if(response!=null){
                response.close();
            }
            httpClient.close();
        }
        return resultString;
    }
    /**
     * 以表单的方式提交post
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static final String doPost(String url) throws ClientProtocolException, IOException{
        return doPost(url,null);
    }
    /**
     * 以表单的方式提交post
     * @param url
     * @param param
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static final String doPost(String url,Map<String,String> param) throws ClientProtocolException, IOException{
        return doPost(url,param,null);
    }


    /**
     * 以表单的方式提交Post请求
     * @param url
     * @param param
     * @param headers
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static final String doPost(String url,Map<String,String> param,Map<String,Object> headers) throws ClientProtocolException, IOException{
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String resultString =null;
        CloseableHttpResponse response =null;
        try{
            HttpPost httpPost = new HttpPost(url);
            if(param!=null){
                List<NameValuePair> nameValuePairs = new ArrayList<>();
                for(Map.Entry<String, String> entry:param.entrySet()){
                    nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
                UrlEncodedFormEntity urlEncodedFormEntity =new UrlEncodedFormEntity(nameValuePairs);
                httpPost.setEntity(urlEncodedFormEntity);
            }
            setHeaders(httpPost, headers);
            response=httpClient.execute(httpPost);
            if(response.getStatusLine().getStatusCode()==200){
                resultString=EntityUtils.toString(response.getEntity(),"UTF-8");
            }
        }
        finally{
            if(response!=null){
                response.close();
            }
            httpClient.close();
        }
        return resultString;
    }
    private static final void setHeaders(HttpUriRequest uriRequest,Map<String,Object> headers){
        if(headers==null){
            if(headers!=null){
                for(Map.Entry<String, Object> entry:headers.entrySet()){
                    uriRequest.addHeader(entry.getKey(), entry.getValue().toString());
                }
            }
        }

    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值