zoukankan      html  css  js  c++  java
  • 【工具类】网络请求封装

    网络请求是我们开发中经常用到,也是非常重要的一环。Java环境下网络请求也是不断进化的,最基础的就是使用Socket发送和接收TCP请求。之后Java引入了HttpURLConnection的标准类,大大简化了请求的过程。但是这并不满足人们的日常需求,之后HttpClient和OkHttp之类的第三方封装应运而生。SpringBoot之后,更优雅的HTTP请求方式——RestTemplate诞生。后来分布式的需求越来越广泛了,Ribbon、Eureka、Feign等具有异步响应的框架也加入其中。

    这就是网络请求的基本发展历程,接下来让我们一个一个看吧。

    HttpURLConnection

    Socket就不说了,感兴趣的可以研究一下。

    package tool.http;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.List;
    import java.util.Map;
    
    public class HTTPURLConnection {
        /**
         * 向指定URL发送GET方法的请求
         *
         * @param url:发送请求的URL
         * @param param:请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
         * @return URL 所代表远程资源的响应结果
         */
        public static String sendGet(String url, String param, Map<String, String> headers) {
            // 返回结果字符串
            String result = "";
            BufferedReader in = null;
            try {
                // 创建远程url连接对象
                String urlNameString = url + "?" + param;
                URL realUrl = new URL(urlNameString);
                // 打开和URL之间的连接
                HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
                // 设置连接方式:get
                connection.setRequestMethod("GET");
                // 设置连接主机服务器的超时时间:15000毫秒
                connection.setConnectTimeout(15000);
                // 设置读取远程返回的数据时间:60000毫秒
                connection.setReadTimeout(60000);
                // 设置通用的请求属性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent",
                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                // 自定义请求头
                for (String key :
                        headers.keySet()) {
                    connection.setRequestProperty(key, headers.get(key));
                }
                // 建立实际的连接
                connection.connect();
                // 获取所有响应头字段
                Map<String, List<String>> map = connection.getHeaderFields();
                // 遍历所有的响应头字段
                for (String key : map.keySet()) {
                    System.out.println(key + "--->" + map.get(key));
                }
                // 定义 BufferedReader输入流来读取URL的响应
                in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                // 存放数据
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                System.out.println("发送GET请求出现异常!" + e);
                e.printStackTrace();
            }
            // 使用finally块来关闭输入流
            finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            return result;
        }
    
    
        /**
         * 向指定 URL 发送POST方法的请求
         *
         * @param url:发送请求的          URL
         * @param param:请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
         * @return 所代表远程资源的响应结果
         */
        public static String sendPost(String url, String param) {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            try {
                URL realUrl = new URL(url);
                // 通过远程url连接对象打开连接
                HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
                // 设置连接请求方式
                connection.setRequestMethod("POST");
                // 设置连接主机服务器超时时间:15000毫秒
                connection.setConnectTimeout(15000);
                // 设置读取主机服务器返回数据超时时间:60000毫秒
                connection.setReadTimeout(60000);
                // 发送POST请求必须设置如下两行
                // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
                connection.setDoOutput(true);
                // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
                connection.setDoInput(true);
                // 设置通用的请求属性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent",
                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                // 获取URLConnection对象对应的输出流
                out = new PrintWriter(connection.getOutputStream());
                // 发送请求参数
                out.print(param);
                // flush输出流的缓冲
                out.flush();
                // 定义BufferedReader输入流来读取URL的响应
                in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                System.out.println("发送 POST 请求出现异常!" + e);
                e.printStackTrace();
            }
            //使用finally块来关闭输出流、输入流
            finally {
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            return result;
        }
    }
    

    HttpClient

    第三方框架主要讲HttpClient,HttpClient有两个比较重要的版本3.1和4.5。HttpClient3.1 已不再更新,但实现工作中使用httpClient3.1的代码还是很多,HttpClient4.5 最新的版本。

    首先你需要Maven依赖:

            <!--httpclient-->
            <dependency>
                <groupId>commons-httpclient</groupId>
                <artifactId>commons-httpclient</artifactId>
                <version>3.1</version>
            </dependency>
    

    封装类

    package tool.http;
    
    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    
    import java.io.*;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    public class HTTPClient3 {
    
        public static String doGet(String url) {
            // 输入流
            InputStream is = null;
            BufferedReader br = null;
            String result = null;
            // 创建httpClient实例
            HttpClient httpClient = new HttpClient();
            // 设置http连接主机服务超时时间:15000毫秒
            // 先获取连接管理器对象,再获取参数对象,再进行参数的赋值
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
            // 创建一个Get方法实例对象
            GetMethod getMethod = new GetMethod(url);
            // 设置get请求超时为60000毫秒
            getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
            // 设置请求重试机制,默认重试次数:3次,参数设置为true,重试机制可用,false相反
            getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));
            try {
                // 执行Get方法
                int statusCode = httpClient.executeMethod(getMethod);
                // 判断返回码
                if (statusCode != HttpStatus.SC_OK) {
                    // 如果状态码返回的不是ok,说明失败了,打印错误信息
                    System.err.println("Method faild: " + getMethod.getStatusLine());
                } else {
                    // 通过getMethod实例,获取远程的一个输入流
                    is = getMethod.getResponseBodyAsStream();
                    // 包装输入流
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    
                    StringBuffer sbf = new StringBuffer();
                    // 读取封装的输入流
                    String temp = null;
                    while ((temp = br.readLine()) != null) {
                        sbf.append(temp).append("
    ");
                    }
    
                    result = sbf.toString();
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                if (null != br) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != is) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                // 释放连接
                getMethod.releaseConnection();
            }
            return result;
        }
    
        public static String doPost(String url, Map<String, Object> paramMap) {
            // 获取输入流
            InputStream is = null;
            BufferedReader br = null;
            String result = null;
            // 创建httpClient实例对象
            org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
            // 设置httpClient连接主机服务器超时时间:15000毫秒
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
            // 创建post请求方法实例对象
            PostMethod postMethod = new PostMethod(url);
            // 设置post请求超时时间
            postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
    
            NameValuePair[] nvp = null;
            // 判断参数map集合paramMap是否为空
            if (null != paramMap && paramMap.size() > 0) {// 不为空
                // 创建键值参数对象数组,大小为参数的个数
                nvp = new NameValuePair[paramMap.size()];
                // 循环遍历参数集合map
                Set<Entry<String, Object>> entrySet = paramMap.entrySet();
                // 获取迭代器
                Iterator<Entry<String, Object>> iterator = entrySet.iterator();
    
                int index = 0;
                while (iterator.hasNext()) {
                    Entry<String, Object> mapEntry = iterator.next();
                    // 从mapEntry中获取key和value创建键值对象存放到数组中
                    try {
                        nvp[index] = new NameValuePair(mapEntry.getKey(),
                                new String(mapEntry.getValue().toString().getBytes("UTF-8"), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    index++;
                }
            }
            // 判断nvp数组是否为空
            if (null != nvp && nvp.length > 0) {
                // 将参数存放到requestBody对象中
                postMethod.setRequestBody(nvp);
            }
            // 执行POST方法
            try {
                int statusCode = httpClient.executeMethod(postMethod);
                // 判断是否成功
                if (statusCode != HttpStatus.SC_OK) {
                    System.err.println("Method faild: " + postMethod.getStatusLine());
                }
                // 获取远程返回的数据
                is = postMethod.getResponseBodyAsStream();
                // 封装输入流
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp).append("
    ");
                }
    
                result = sbf.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                if (null != br) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != is) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                // 释放连接
                postMethod.releaseConnection();
            }
            return result;
        }
    }
    
    

    4.5 Version

    package com.powerX.httpClient;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    public class HttpClient4 {
    
        public static String doGet(String url) {
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse response = null;
            String result = "";
            try {
                // 通过址默认配置创建一个httpClient实例
                httpClient = HttpClients.createDefault();
                // 创建httpGet远程连接实例
                HttpGet httpGet = new HttpGet(url);
                // 设置请求头信息,鉴权
                httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
                // 设置配置请求参数
                RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                        .setConnectionRequestTimeout(35000)// 请求超时时间
                        .setSocketTimeout(60000)// 数据读取超时时间
                        .build();
                // 为httpGet实例设置配置
                httpGet.setConfig(requestConfig);
                // 执行get请求得到返回对象
                response = httpClient.execute(httpGet);
                // 通过返回对象获取返回数据
                HttpEntity entity = response.getEntity();
                // 通过EntityUtils中的toString方法将结果转换为字符串
                result = EntityUtils.toString(entity);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                if (null != response) {
                    try {
                        response.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != httpClient) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return result;
        }
    
        public static String doPost(String url, Map<String, Object> paramMap) {
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse httpResponse = null;
            String result = "";
            // 创建httpClient实例
            httpClient = HttpClients.createDefault();
            // 创建httpPost远程连接实例
            HttpPost httpPost = new HttpPost(url);
            // 配置请求参数实例
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间
                    .setConnectionRequestTimeout(35000)// 设置连接请求超时时间
                    .setSocketTimeout(60000)// 设置读取数据连接超时时间
                    .build();
            // 为httpPost实例设置配置
            httpPost.setConfig(requestConfig);
            // 设置请求头
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            // 封装post请求参数
            if (null != paramMap && paramMap.size() > 0) {
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                // 通过map集成entrySet方法获取entity
                Set<Entry<String, Object>> entrySet = paramMap.entrySet();
                // 循环遍历,获取迭代器
                Iterator<Entry<String, Object>> iterator = entrySet.iterator();
                while (iterator.hasNext()) {
                    Entry<String, Object> mapEntry = iterator.next();
                    nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString()));
                }
    
                // 为httpPost设置封装好的请求参数
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            try {
                // httpClient对象执行post请求,并返回响应参数对象
                httpResponse = httpClient.execute(httpPost);
                // 从响应对象中获取响应内容
                HttpEntity entity = httpResponse.getEntity();
                result = EntityUtils.toString(entity);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                if (null != httpResponse) {
                    try {
                        httpResponse.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != httpClient) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return result;
        }
    }
    

    RestTemplate

    RestTemplate是SpringBoot对网络请求的封装,使用之后再也不需要繁琐的调用了。使用RestTemplate前,得了解几个实体。
    ResponseEntity:请求返回值
    BodyBuilder:构造器
    HttpStatus:返回的请求状态
    HttpEntity:Http请求的请求体
    HttpHeaders:Http请求的请求头

    再记住RestTemplate中和RestFul请求风格的对应方法即可。具体如下:

    HTTP method RestTemplate methods
    DELETE delete
    GET getForObject
    getForEntity
    HEAD headForHeaders
    OPTIONS optionsForAllow
    POST postForLocation
    postForObject
    PUT put
    any exchange
    execute

    细节就不多说了。

    分布式网络请求框架

    Eureka

    Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。服务注册与发现对于微服务架构来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了。功能类似于dubbo的注册中心,比如Zookeeper。

    Ribbon

    Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具。简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们也很容易使用Ribbon实现自定义的负载均衡算法。

    Feign

    Feign是一个声明式的Web服务客户端,使得编写Web服务客户端变得非常容易,只需要创建一个接口,然后在上面添加注解即可,同时也支持JAX-RS标准的注解。Feign旨在使编写Java Http客户端变得更容易。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

    在Feign之前,使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它,即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。

    Feign通过接口的方法调用Rest服务(之前是Ribbon+RestTemplate),该请求发送给Eureka服务器, 通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。

    Json处理工具——fastjson

    现在接口之间交互我们大都使用Json,市面上有很多Json处理工具,例如alibaba的fastjson,apache的Jackson,Google的Gson。用哪个都差不多,国内用fastjson比较多。

    参考文献

    https://www.cnblogs.com/hhhshct/p/8523697.html
    https://www.jianshu.com/p/27a82c494413
    https://blog.csdn.net/qq_41234832/article/details/84864551
    https://www.cnblogs.com/cjsblog/p/9858154.html
    https://blog.csdn.net/huxiutao/article/details/87927045
    https://maizitoday.github.io/post/springcloud系列-ribbon/
    https://www.w3cschool.cn/fastjson/fastjson-ex2.html
    https://zhuanlan.zhihu.com/p/146540159

  • 相关阅读:
    熟练掌握js中this的用法,解析this在不同应用场景的作用
    用css3绘制你需要的几何图形
    js中Prototype属性解释及常用方法
    Nodejs之MEAN栈开发(九)---- 用户评论的增加/删除/修改
    深入理解Angular中的$Apply()以及$Digest()
    全面理解JavaScript中的闭包的含义及用法
    详解javascript,ES5标准中新增的几种高效Array操作方法
    区别和详解:js中call()和apply()的用法
    框架基础:ajax设计方案(一)---集成核心请求
    jQuery点击图片弹出大图遮罩层
  • 原文地址:https://www.cnblogs.com/zllk/p/14899892.html
Copyright © 2011-2022 走看看