HttpClient学习笔记

1、简介

        HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTML Unit都使用了 HttpClient。

2、功能介绍

以下列出的是 HttpClient 提供的主要的功能,要知道更多详细的功能可以参见 HttpClient 的官网:

官方网站:http://hc.apache.org/httpcomponents-client-4.5.x/index.html

  • 基于标准的纯Java,HTTP版本1.0和1.1的实现
  • 在可扩展的OO框架中完全实现所有HTTP方法(GET,POST,PUT,DELETE,HEAD,OPTIONS和TRACE)。
  • 支持使用HTTPS(HTTP over SSL)协议进行加密。
  • 通过HTTP代理进行透明连接。
  • 通过HTTP代理通过CONNECT方法进行隧道连接。
  • 基本,摘要,NTLMv1,NTLMv2,NTLM2会话,SNPNEGO,Kerberos身份验证方案。
  • 用于自定义身份验证方案的插件机制。
  • 可插拔安全插座工厂,使其更易于使用第三方解决方案
  • 连接管理支持在多线程应用程序中使用。支持设置最大总连接数以及每个主机的最大连接数。检测并关闭陈旧连接。
  • 用于读取Set-Cookie的自动Cookie处理:来自服务器的标头,并在适当时将其发送回Cookie:标头。
  • 用于自定义cookie策略的插件机制。
  • 请求输出流以避免通过直接流式传输到服务器的套接字来缓冲任何内容主体。
  • 响应输入流通过直接从套接字流式传输到服务器来有效地读取响应主体。
  • 使用HTTP / 1.0中的KeepAlive和HTTP / 1.1中的持久连接的持久连接
  • 直接访问服务器发送的响应代码和标头。
  • 设置连接超时的能力。
  • 支持HTTP / 1.1响应缓存。
  • 源代码可在Apache许可下免费获得。

 3、使用

导入Maven依赖:

<!-- 添加Httpclient支持 -->
    <dependency>
	    <groupId>org.apache.httpcomponents</groupId>
	    <artifactId>httpclient</artifactId>
	    <version>4.5.2</version>
	</dependency>

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

1. 创建HttpClient对象。

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. 释放连接。无论执行方法是否成功,都必须释放连接

代码示例:

 1、发送Get请求无参:

    @org.junit.Test
	public void test() {
		get();
		//getWithParams();
		//postLogin();
	}    

     /**
	 * 发送 get请求
	 */
	public void get() {
	
		//创建httpClient实例
		CloseableHttpClient httpClient=HttpClients.createDefault();
		//创建httpGet
		HttpGet httpGet=new HttpGet("https://www.cnblogs.com/");
		System.out.println("executing request:" + httpGet.getURI()); 
		//设置http的状态参数
		RequestConfig config=RequestConfig.custom()
					.setSocketTimeout(10000) // 设置读取超时时间
					.setConnectTimeout(5000) // 设置连接超时时间
					.setConnectionRequestTimeout(5000)// 设置请求超时时间(单位毫秒)
					.build();
		httpGet.setConfig(config);
		
		//创建response响应
		CloseableHttpResponse response=null;
		try {
			//执行get请求
			response=httpClient.execute(httpGet);
			//获取响应的实体
			HttpEntity entity=response.getEntity();
			System.out.println("--------------------------------------");
			// 打印响应内容长度     
            System.out.println("Response content length: " + entity.getContentLength());
			// 打印响应状态  
			System.out.println(response.getStatusLine());
			System.out.println(response.getStatusLine().getStatusCode());
			if(entity!=null){
				//输出响应结果
				System.out.println("Response Content:"+"\n"+EntityUtils.toString(entity,"utf-8"));
			}
			System.out.println("------------------------------------");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			//关闭连接,释放资源
			try {
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

2、发送Get有参:

/**
	 * get请求有参数
	 */
	public void getWithParams() {
		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		// 参数
		URI uri = null;
		try {
			// 将参数放入键值对类NameValuePair中,再放入集合中
			List<NameValuePair> params = new ArrayList<>();
			params.add(new BasicNameValuePair("userName", "admin"));
			params.add(new BasicNameValuePair("password", "123456"));
			// 设置uri信息,并将参数集合放入uri;
			// 注:这里也支持一个键值对一个键值对地往里面放setParameter(String key, String value)
			// http://localhost:8080/user/getWithParams.do?userName=admin&password=123456
			uri = new URIBuilder().setScheme("http")
					      .setHost("localhost")
					     .setPort(8080)
					     .setPath("/user/getWithParams.do")
					     .setParameters(params)
					     .build();
		} catch (URISyntaxException e1) {
			e1.printStackTrace();
		}
		// 创建Get请求
		HttpGet httpGet = new HttpGet(uri);
		System.out.println("executing request:" + httpGet.getURI());
		// 响应模型
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Get请求
			response = httpClient.execute(httpGet);
			// 配置信息
			RequestConfig requestConfig = RequestConfig.custom()
					// 设置连接超时时间(单位毫秒)
					.setConnectTimeout(5000)
					// 设置请求超时时间(单位毫秒)
					.setConnectionRequestTimeout(5000)
					// socket读写超时时间(单位毫秒)
					.setSocketTimeout(5000)
					// 设置是否允许重定向(默认为true)
					.setRedirectsEnabled(true).build();
			// 将上面的配置信息 运用到这个Get请求里
			httpGet.setConfig(requestConfig);
			// 从响应模型中获取响应实体
			HttpEntity entity = response.getEntity();
			System.out.println("响应状态为:" + response.getStatusLine());
			if (entity != null) {
				System.out.println("响应内容长度为:" + entity.getContentLength());
				System.out.println("响应内容为:" + EntityUtils.toString(entity,"utf-8"));
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

我的Controller测试代码如下:

	@ResponseBody
	@RequestMapping("/getWithParams")
	public String testGet(String userName,String password){
		return "userName"+userName+"password:"+password;
	}

 响应后的结果:

3、发送Post请求(模拟用户登录请求):

/**
	 * post方式提交表单(模拟用户登录请求)
	 */
	public void postLogin() {
		// 创建httpClient实例
		CloseableHttpClient httpClient = HttpClients.createDefault();
		// 创建httppost  
		HttpPost httpPost = new HttpPost("http://localhost:8080/user/postWithParams.do");
		// 创建参数队列  
		List list = new ArrayList();
		list.add(new BasicNameValuePair("userName", "admin"));
		list.add(new BasicNameValuePair("password", "123456"));
		UrlEncodedFormEntity uefEntity;
		try {
			uefEntity = new UrlEncodedFormEntity(list, "UTF-8");
			httpPost.setEntity(uefEntity);
			System.out.println("executing request " + httpPost.getURI());
			CloseableHttpResponse response = httpClient.execute(httpPost);
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					System.out.println("--------------------------------------");
					System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
					System.out.println("--------------------------------------");
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源  
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
        @ResponseBody
	@RequestMapping("/postWithParams")
	public String testPost(String userName,String password){
		return "userName="+userName+"----password="+password;
	}

返回的结果为:

更多更详细可查看: https://blog.csdn.net/qq_42969074/article/details/85561394

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值