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

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。博客源地址为zhixiang.org.cn https://blog.csdn.net/myFirstCN/article/details/80880234 使用之前首先添加maven依赖或者是jar包 org.
版权声明:本文为博主原创文章,未经博主允许不得转载。博客源地址为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;
        }
    }
}

相关文章
|
2天前
|
XML 网络协议 Java
JavaWeb -- HTTP -- WEB服务器TOMCAT
JavaWeb -- HTTP -- WEB服务器TOMCAT
|
3天前
|
Web App开发 缓存 JavaScript
使用TypeScript创建高效HTTP代理请求
使用TypeScript创建高效HTTP代理请求
|
22小时前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
【6月更文挑战第15天】Python的requests库简化了HTTP请求。安装后,使用`requests.get()`发送GET请求,检查`status_code`为200表示成功。类似地,`requests.post()`用于POST请求,需提供JSON数据和`Content-Type`头。
9 6
|
2天前
|
Python
python做http请求
python做http请求
9 1
|
7天前
|
JSON API 数据格式
Requests库:轻松实现Python中的HTTP请求
Requests是Python的第三方HTTP库,简化了HTTP请求的发送,支持GET、POST等方法。要安装,使用`pip install requests`。Requests以其简洁API和强大功能成为网络编程首选工具,为开发者提供高效稳定的网络交互体验。
20 5
|
8天前
|
API Python
Python HTTP请求库对比,以实战请求豆瓣排行榜为例
对比了Python的几个HTTP请求库,包括`requests`、`http.client`、`aiohttp`、`urllib`、`httpx`、`treq`和`requests-toolbelt`,各有特点和优缺点。选择时应考虑项目需求(如异步支持)、易用性、社区支持、性能和兼容性。示例展示了如何使用`requests`和`aiohttp`库发送豆瓣电影排行榜的GET请求。
8 0
|
8天前
|
JSON 安全 Java
JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
JAVA Socket 实现HTTP与HTTPS客户端发送POST与GET方式请求
13 0
|
9天前
|
移动开发 Java
Java Socket编程 - 基于Socket实现HTTP下载客户端
Java Socket编程 - 基于Socket实现HTTP下载客户端
14 1
|
9天前
流量回放工具之 GoReplay output-http-stats(HTTP请求统计) 源码分析
【6月更文挑战4天】流量回放工具之 GoReplay output-http-stats(HTTP请求统计) 源码分析
16 4
|
Java 调度
Java服务器宕机解决方法论(下)
Java服务器宕机解决方法论(下)
331 0