HttpClient4.x之Get请求示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ljk126wy/article/details/84070334

在使用的HttpClient的之前先了解一下它是什么,为了尽可能的展示其最为原味的介绍,我们就去他的官网看看。http://hc.apache.org/httpcomponents-client-ga/  首页繁体繁体内容如下:

该图片内容是通过谷歌游览器自带翻译插件进行翻译的内容。

我在实际工作中一般进行后台跨域和不同服务接口调用使用比较频繁,自己拿他做过一些简单的爬虫。后面会给大家写一篇实现百度搜索的爬虫。

开始我们的博客主题内容:。获取请求示例,在开始介绍之前我们需要进行一写准备工作确保已经安装JDK和Maven的环境,并对JDK和行家有一定的了解另外开发工具这里我这里使用的是Spring Tool Suite(STS)(当然你也可以使用其他的开发工具进行。环境和版本说明大致如下:

开发工具:Spring Tool Suite(STS)   3.9.6.RELEASE

maven版本:3.2.5

jdk版本: 1.8.0_144

首先添加的的HttpClient的依赖

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

引入的log4j的的依赖

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

log4j.properties

log4j.rootLogger=INFO,stdout


### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

 获取请求示例操作流程

1我们要先创建HttpClient的对象

2创建HTTPGET对象并通过构造设置访问的网址以及调用参数

3执行的HttpClient的执行方法发起GET请求并返回CloseableHttpResponse对象

4从CloseableHttpResponse对象中获取响应状态以及响应内容。

 获得请求示例演示程序详细代码:

package cn.zhuoqianmingyue.getoperation;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class SimpleGetHttpClientDemo {
	
	private static Log log  =  LogFactory.getLog(SimpleGetHttpClientDemo.class );   
	
	/**
	 *  无参数的get访问
	 */
	@Test
	public void withoutParameters() {
		//创建HttpClinet
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//添加HTTP GET请求 访问百度首页
		HttpGet httpGet = new HttpGet("https://www.baidu.com");
		CloseableHttpResponse response = null;
		try {
			//执行请求访问
			response = httpClient.execute(httpGet);
			//获取返回HTTP状态码
			int satausCode = response.getStatusLine().getStatusCode();
			if(satausCode == 200 ){
				String content = EntityUtils.toString(response.getEntity(),"UTF-8");
				log.info("百度首页页面:"+content);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 *  有参数的访问
	 * @throws URISyntaxException
	 */
	@Test
	public void withParameters() throws URISyntaxException {

		//创建HttpClinet
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//拼接访问url 进行
		URI uri = new URI("http://www.baidu.com/s");
		//拼接搜索内容 ?wd=httpclinet
		URIBuilder uriBuilder = new URIBuilder(uri);
		uriBuilder.setParameter("wd", "httpclient");
		URI uriParma = uriBuilder.build();
		//添加HTTP GET请求 访问百度搜索httpclient相关信息
		HttpGet httpGet = new HttpGet(uriParma);
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			int satausCode = response.getStatusLine().getStatusCode();
			if(satausCode == 200 ){
				String content = EntityUtils.toString(response.getEntity(),"UTF-8");
				log.info(content);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
	/**
	 *  访问https://www.baidu.com 搜索需要设置请求头的 Host:www.baidu.com
	 * @throws URISyntaxException
	 */
	@Test
	public void withParametersByHttps() throws URISyntaxException {

		//创建HttpClinet
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//拼接访问url 进行
		URI uri = new URI("https://www.baidu.com/s");
		//拼接搜索内容 ?wd=httpclinet
		URIBuilder uriBuilder = new URIBuilder(uri);
		uriBuilder.setParameter("wd", "httpclient");
		URI uriParma = uriBuilder.build();
		//添加HTTP GET请求 访问百度搜索httpclient相关信息
		HttpGet httpGet = new HttpGet(uriParma);
		
		httpGet.addHeader("Host","www.baidu.com");
		httpGet.addHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			int satausCode = response.getStatusLine().getStatusCode();
			if(satausCode == 200 ){
				String content = EntityUtils.toString(response.getEntity(),"UTF-8");
				log.info(content);
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

源码地址:https//github.com/zhuoqianmingyue/httpclientexamples

猜你喜欢

转载自blog.csdn.net/ljk126wy/article/details/84070334