HttpClient_入门案例

编程入门 行业动态 更新时间:2024-10-22 19:45:43

HttpClient_<a href=https://www.elefans.com/category/jswz/34/1770026.html style=入门案例"/>

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

特点:

  1. HttpClient别名:HttpComponents
  2. 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_入门案例

本文发布于:2023-07-27 22:08:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:入门   案例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!