Java工具类之在服务器发送HTTP请求

版权声明:本文为博主原创文章,未经博主允许不得转载。博客源地址为zhixiang.org.cn https://blog.csdn.net/myFirstCN/article/details/80880234


使用之前首先添加maven依赖或者是jar包

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

下面给出工具类代码,一个简单发送Get和Post请求的例子

public class HttpUtils {
   
    /*
     *发送简单post请求
     */
    public static JSONObject post(String url) {
        HttpPost post = new HttpPost(url);
        return getResult(post);
    }
    /*
     *发送带Header的post请求
     */
    public static JSONObject post(String url, Map<String, String> map) {
        HttpPost post = new HttpPost(url);
        if (!map.isEmpty()) {
            Set<Map.Entry<String, String>> entrys = map.entrySet();
            for (Map.Entry<String, String> entry : entrys) {
                post.setHeader(entry.getKey(), entry.getValue());
            }
        }
        return getResult(post);
    }
    /*
     *发送带Header的get请求
     */
    public static JSONObject get(String url, Map<String, String> map) {
        HttpGet get = new HttpGet(url);
        if (!map.isEmpty()) {
            Set<Map.Entry<String, String>> entrys = map.entrySet();
            for (Map.Entry<String, String> entry : entrys) {
                get.setHeader(entry.getKey(), entry.getValue());
            }
        }
        return getResult(get);

    }
    /*
     *发送简单的get请求
     */
    public static JSONObject get(String url) {
        HttpGet get = new HttpGet(url);
        return getResult(get);

    }
    /*
     *发送请求方法,请求响应为JSONObject
     */
    private static JSONObject getResult(HttpRequestBase requestBase) {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        String result = null;
        try {
            result = EntityUtils.toString(httpClient.execute(requestBase).getEntity());
            httpClient.close();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            return new JSONObject(JSON.parseObject(result));
        }
    }
    /*
     *当请求响应为String时
     */
    public static String getString(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        String result = null;
        try {
            result = EntityUtils.toString(httpClient.execute(get).getEntity());
            httpClient.close();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            return result;
        }
    }
}

使用之前首先添加maven依赖或者是jar包

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

下面给出工具类代码,一个简单发送Get和Post请求的例子

public class HttpUtils {
   
    /*
     *发送简单post请求
     */
    public static JSONObject post(String url) {
        HttpPost post = new HttpPost(url);
        return getResult(post);
    }
    /*
     *发送带Header的post请求
     */
    public static JSONObject post(String url, Map<String, String> map) {
        HttpPost post = new HttpPost(url);
        if (!map.isEmpty()) {
            Set<Map.Entry<String, String>> entrys = map.entrySet();
            for (Map.Entry<String, String> entry : entrys) {
                post.setHeader(entry.getKey(), entry.getValue());
            }
        }
        return getResult(post);
    }
    /*
     *发送带Header的get请求
     */
    public static JSONObject get(String url, Map<String, String> map) {
        HttpGet get = new HttpGet(url);
        if (!map.isEmpty()) {
            Set<Map.Entry<String, String>> entrys = map.entrySet();
            for (Map.Entry<String, String> entry : entrys) {
                get.setHeader(entry.getKey(), entry.getValue());
            }
        }
        return getResult(get);

    }
    /*
     *发送简单的get请求
     */
    public static JSONObject get(String url) {
        HttpGet get = new HttpGet(url);
        return getResult(get);

    }
    /*
     *发送请求方法,请求响应为JSONObject
     */
    private static JSONObject getResult(HttpRequestBase requestBase) {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        String result = null;
        try {
            result = EntityUtils.toString(httpClient.execute(requestBase).getEntity());
            httpClient.close();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            return new JSONObject(JSON.parseObject(result));
        }
    }
    /*
     *当请求响应为String时
     */
    public static String getString(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        String result = null;
        try {
            result = EntityUtils.toString(httpClient.execute(get).getEntity());
            httpClient.close();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            return result;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值