Java请求Http接口(Http请求-HttpClient方法)

Table of Contents

一、简介

二、特性

三、使用方法

四:示例:

1:导包:

2:get请求

3:post请求

4:HttpClient 和CloseableHttpClient 区别


Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。因此熟练掌握HttpClient是很重要的必修内容,掌握HttpClient后,相信对于Http协议的了解会更加深入。

一、简介

HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。

下载地址: http://hc.apache.org/downloads.cgi

二、特性


1. 基于标准、纯净的java语言。实现了Http1.0和Http1.1

2. 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。

3. 支持HTTPS协议。

4. 通过Http代理建立透明的连接。

5. 利用CONNECT方法通过Http代理建立隧道的https连接。

6. Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos认证方案。

7. 插件式的自定义认证方案。

8. 便携可靠的套接字工厂使它更容易的使用第三方解决方案。

9. 连接管理器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接。

10. 自动处理Set-Cookie中的Cookie。

11. 插件式的自定义Cookie策略。

12. Request的输出流可以避免流中内容直接缓冲到socket服务器。

13. Response的输入流可以有效的从socket服务器直接读取相应内容。

14. 在http1.0和http1.1中利用KeepAlive保持持久连接。

15. 直接获取服务器发送的response code和 headers。

16. 设置连接超时的能力。

17. 实验性的支持http1.1 response caching。

18. 源代码基于Apache License 可免费获取。

三、使用方法


使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

1. 创建HttpClient对象。最新版的httpClient使用实现类的是closeableHTTPClient,以前的default作废了.

2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。

4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。

5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。

6. 释放连接。无论执行方法是否成功,都必须释放连接

环境说明:Eclipse、JDK1.8、SpringBoot

四:示例:

1:导包:

        <!--httpclient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.12</version>
        </dependency>

        <!--fafastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.71</version>
        </dependency>


注:本人引入此依赖的目的是,在后续示例中,会用到“将对象转化为json字符串的功能”,也可以引其他有此功能的依赖。 

详细使用示例
声明:此示例中,以JAVA发送HttpClient(在test里面单元测试发送的);也是以JAVA接收的(在controller里面接收的)。

声明:下面的代码,本人亲测有效。

2:get请求

 /**
     * 发送get请求
     * @param url  请求路径
     * @param params  请求参数,格式为key-value
     * @param header  请求头
     * @return
     */
    public String getData(String url, Map<String, String> params, Map<String, String> header) {
        String responeStr = null;
        //获取Http客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //返回模型
        CloseableHttpResponse response = null;

        try {
            //获取url的请求路径
            URIBuilder uriBuilder = new URIBuilder(url);
            //获取参数的迭代器
            Iterator paramsIterator = params.entrySet().iterator();

            //迭代参数,将参数挂在url后
            while(paramsIterator.hasNext()) {
                Map.Entry entry = (Map.Entry)paramsIterator.next();
                uriBuilder.addParameter((String)entry.getKey(), (String)entry.getValue());
            }

            //创建httpget请求
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            //判断请求头是否为空
            if (header != null) {
                Iterator headerIterator = header.entrySet().iterator();

                //封装请求头
                while(headerIterator.hasNext()) {
                    Map.Entry headerEntrty = (Map.Entry)headerIterator.next();
                    httpGet.setHeader((String)headerEntrty.getKey(), (String)headerEntrty.getValue());
                }
            }

            //发送请求
            response = httpClient.execute(httpGet);
            //获取并判断请求
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = response.getEntity();
                responeStr = this.entityToString(httpEntity);
            }

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

        }

        return null;
    }





/**
     * 解析HttpEntity
     * @param entity
     * @return
     * @throws IOException
     */
    private String entityToString(HttpEntity entity) throws IOException {
        String result = null;
        InputStreamReader inputStreamReader = null;
        try {
            if (entity != null) {
                long contentLength = entity.getContentLength();
                if (contentLength != -1L && contentLength < 2048L) {
                    result = EntityUtils.toString(entity, "UTF-8");
                } else {
                    inputStreamReader = new InputStreamReader(entity.getContent(), "UTF-8");
                    CharArrayBuffer charArrayBuffer = new CharArrayBuffer(2048);
                    char[] chars = new char[1024];

                    int index;
                    while((index = inputStreamReader.read(chars)) != -1) {
                        charArrayBuffer.append(chars, 0, index);
                    }

                    result = charArrayBuffer.toString();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputStreamReader.close();
        }

        return result;
    }

3:post请求

    /**
     * 发起post请求
     * @param URL  请求地址
     * @param params  参数 格式为map
     * @param header 请求头,可以为null
     * @return
     */
    public String postData(String URL, Map<String, String> params, Map<String, String> header) {
        String respStr = null;
        //获取Http客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建post请求
        HttpPost httpPost = new HttpPost(URL);
        //存储参数的BasicNameValuePair集合
        ArrayList list = new ArrayList();

        //获得参数的迭代器
        Iterator iterator = params.entrySet().iterator();

        //迭代参数
        while(iterator.hasNext()) {
            Map.Entry entry = (Map.Entry)iterator.next();
            //BasicNameValuePair通常是用来封装post请求中的参数名称和值;
            list.add(new BasicNameValuePair((String)entry.getKey(), (String)entry.getValue()));
        }

        //请求模型
        CloseableHttpResponse response = null;

        try {
            /*两个键值对,被UrlEncodedFormEntity实例编码后变为如下内容:
            param1=value1&param2=value2
            然后将请求参数放进Entity中*/
            httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
            //如果请求头不为null
            if (header != null) {
                Iterator headerIterator = header.entrySet().iterator();

                //就遍历请求头中的参数
                while(headerIterator.hasNext()) {
                    Map.Entry headerEntry = (Map.Entry)headerIterator.next();
                    httpPost.setHeader((String)headerEntry.getKey(), (String)headerEntry.getValue());
                }
            }

            //发送post请求
            response = httpClient.execute(httpPost);
            //如果请求不为空并且响应code为200
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                //获取请求模型返回的实体
                HttpEntity responseEntity = response.getEntity();
                respStr = this.entityToString(responseEntity);
            }

            String resultStr = respStr;
            return resultStr;
        } catch (UnsupportedEncodingException var23) {
            var23.printStackTrace();
        } catch (ClientProtocolException var24) {
            var24.printStackTrace();
        } catch (IOException var25) {
            var25.printStackTrace();
        } finally {
            try {
                httpClient.close();
                if (response != null) {
                    response.close();
                }
            } catch (IOException var22) {
                var22.printStackTrace();
            }

        }

        return null;
    }



/**
     * 解析HttpEntity
     * @param entity
     * @return
     * @throws IOException
     */
    private String entityToString(HttpEntity entity) throws IOException {
        String result = null;
        InputStreamReader inputStreamReader = null;
        try {
            if (entity != null) {
                long contentLength = entity.getContentLength();
                if (contentLength != -1L && contentLength < 2048L) {
                    result = EntityUtils.toString(entity, "UTF-8");
                } else {
                    inputStreamReader = new InputStreamReader(entity.getContent(), "UTF-8");
                    CharArrayBuffer charArrayBuffer = new CharArrayBuffer(2048);
                    char[] chars = new char[1024];

                    int index;
                    while((index = inputStreamReader.read(chars)) != -1) {
                        charArrayBuffer.append(chars, 0, index);
                    }

                    result = charArrayBuffer.toString();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputStreamReader.close();
        }

        return result;
    }

注:如果要传文件:

        //文件URL,此处取豆瓣上的一个图片
        String fileUrl ="https://img1.doubanio.com/view/photo/l/public/p2537149328.webp";
        
            //提取到文件名
            String fileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
            //转换成文件流
            InputStream is = new URL(fileUrl).openStream();

            //接收文件的服务器地址
            String uploadURL = "http://localhost:8003/fileupload";

            //创建HttpClient
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(uploadURL);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            /*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/
            builder.addBinaryBody("file",is, ContentType.MULTIPART_FORM_DATA,fileName);
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);

4:HttpClient 和CloseableHttpClient 区别

httpclient3.x


		HttpClient client = new HttpClient();
		// 设置代理服务器地址和端口
		// client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);
		// 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https
		HttpMethodmethod = new GetMethod("http://java.sun.com");
		// 使用POST方法
		// HttpMethod method = new PostMethod("http://java.sun.com");
		client.executeMethod(method);
		// 打印服务器返回的状态
		System.out.println(method.getStatusLine());
		// 打印返回的信息
		System.out.println(method.getResponseBodyAsString());
		// 释放连接
		method.releaseConnection();
 

 

httpclient4.x到httpclient4.3以下 


public void getUrl(String url, String encoding) throws ClientProtocolException, IOException {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		HttpResponse response = client.execute(get);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream instream = entity.getContent();
			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(instream, encoding));
				System.out.println(reader.readLine());
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				instream.close();
			}
		}
		// 关闭连接.
		client.getConnectionManager().shutdown();
	}
 

 

 

httpclient4.3以上 


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public static String getResult(String urlStr) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		// HTTP Get请求
		HttpGet httpGet = new HttpGet(urlStr);
		// 设置请求和传输超时时间
		// RequestConfig requestConfig =
		// RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).build();
		// httpGet.setConfig(requestConfig);
		String res = "";
		try {
			// 执行请求
			HttpResponse getAddrResp = httpClient.execute(httpGet);
			HttpEntity entity = getAddrResp.getEntity();
			if (entity != null) {
				res = EntityUtils.toString(entity);
			}
			log.info("响应" + getAddrResp.getStatusLine());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return res;
		} finally {
			try {
				httpClient.close();
			} catch (IOException e) {
				log.error(e.getMessage(), e);
				return res;
			}
		}
		return res;
	}

  • 7
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苍煜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值