入门案例"/>
HttpClient_入门案例
文章目录
- 一、HttpClient
- 1.1、 前台系统访问后台接口的方式
- 1.2、 什么是HttpClient
- 1.3、 HttpClient入门案例
- 1.3.1、 发起Get请求
- 1.3.2、 带参数的Get请求
- 1.3.4、 带参数POST请求
一、HttpClient
1.1、 前台系统访问后台接口的方式
两个系统间如何互相访问?两个tomcat上的项目如何互相访问
采用HttpClient实现跨系统的接口调用。
1.2、 什么是HttpClient
官网:.html
现在也叫:HttpComponents
特点:
- HttpClient别名:HttpComponents
- HttpClient可以发送get、post、put、delete、…等请求
1.3、 HttpClient入门案例
pom.xml和application.properties文件
导入maven坐标
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.4</version>
</dependency>
1.3.1、 发起Get请求
- 流程
1.创建一个客户端 CloseableHttpClient
2.创建一个get方法请求实例 HttpGet
3.发送请求 execute
4.获取响应的头信息
5.获取响应的主题内容
6.关闭响应对象
使用HttpClient发起Get请求的案例代码:
/*** 使用HttpClient发起Get请求,请求百度*/
public class DoGETParam {public static void main(String[] args) throws Exception {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();// 创建URI对象,并且设置请求参数URI uri = new URIBuilder("").setParameter("wd", "java").build();// 创建http GET请求HttpGet httpGet = new HttpGet(uri);// HttpGet get = new HttpGet("=java");CloseableHttpResponse response = null;try {// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {// 解析响应数据String content = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(content);}} finally {if (response != null) {response.close();}httpclient.close();}}
}
执行日志:
请求头:
响应头:
数据:
1.3.2、 带参数的Get请求
流程
1创建一个客户端 CloseableHttpClient
2 通过URIBuilder传递参数
3创建一个get方法请求实例 HttpGet
4发送请求 execute
5获取响应的头信息
6获取响应的主题内容
7关闭响应对象
访问网站的爬虫协议:
public class DoGETParam {public static void main(String[] args) throws Exception {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();// 创建URI对象,并且设置请求参数URI uri = new URIBuilder("").setParameter("wd", "java").build();// 创建http GET请求HttpGet httpGet = new HttpGet(uri);// HttpGet get = new HttpGet("=java");CloseableHttpResponse response = null;try {// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {// 解析响应数据String content = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(content);}} finally {if (response != null) {response.close();}httpclient.close();}}
}
###1.3.3、发起POST请求
/** 演示:使用HttpClient发起POST请求*/
public class DoPOST {public static void main(String[] args) throws Exception {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();// 创建http POST请求HttpPost httpPost = new HttpPost("/");// 把自己伪装成浏览器。否则开源中国会拦截访问httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");CloseableHttpResponse response = null;try {// 执行请求response = httpclient.execute(httpPost);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {// 解析响应数据String content = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(content);}} finally {if (response != null) {response.close();}// 关闭浏览器httpclient.close();}}
}
1.3.4、 带参数POST请求
/** 演示:使用HttpClient发起带有参数的POST请求*/
public class DoPOSTParam {public static void main(String[] args) throws Exception {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();// 创建http POST请求,访问开源中国HttpPost httpPost = new HttpPost("");// 根据开源中国的请求需要,设置post请求参数List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);parameters.add(new BasicNameValuePair("scope", "project"));parameters.add(new BasicNameValuePair("q", "java"));parameters.add(new BasicNameValuePair("fromerr", "8bDnUWwC"));// 构造一个form表单式的实体UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);// 将请求实体设置到httpPost对象中httpPost.setEntity(formEntity);CloseableHttpResponse response = null;try {// 执行请求response = httpclient.execute(httpPost);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == 200) {// 解析响应体String content = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(content);}} finally {if (response != null) {response.close();}// 关闭浏览器httpclient.close();}}
}
更多推荐
HttpClient_入门案例
发布评论