HttpClient设置请求头消息User-Agent模拟浏览器

HttpClient设置请求头消息User-Agent模拟浏览器
  比如我们请求 www.tuicool.com
  用前面的代码
  这个是一个Maven项目Pom.xml配置如下

			<?xml version="1.0" encoding="UTF-8"?>
			<project xmlns="http://maven.apache.org/POM/4.0.0"
			         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
			    <modelVersion>4.0.0</modelVersion>
			
			    <groupId>com.xsh</groupId>
			    <artifactId>http</artifactId>
			    <version>1.0</version>
			
			    <dependencies>
			
			        <dependency>
			            <groupId>org.springframework.boot</groupId>
			            <artifactId>spring-boot-starter-web</artifactId>
			            <version>1.5.8.RELEASE</version>
			        </dependency>
			
			        <!-- 基础包的依赖-->
			        <dependency>
			            <groupId>javax.servlet</groupId>
			            <artifactId>javax.servlet-api</artifactId>
			            <version>3.1.0</version>
			        </dependency>
			
			
			        <dependency>
			            <groupId>org.springframework</groupId>
			            <artifactId>spring-context</artifactId>
			            <version>4.3.5.RELEASE</version>
			        </dependency>
			
			        <dependency>
			            <groupId>org.apache.httpcomponents</groupId>
			            <artifactId>httpcore</artifactId>
			            <version>4.4.5</version>
			        </dependency>
			
			        <dependency>
			            <groupId>org.apache.httpcomponents</groupId>
			            <artifactId>httpclient</artifactId>
			            <version>4.5.1</version>
			        </dependency>
			    </dependencies>
			
			</project>

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

/**
 * @author Zerox
 * @date 2019/2/21 8:38
 */
public class Demo1Test {

    /**
     *  1. 第一次执行之后检测到没有请求头~
     */
    public static void main(String[] args) throws Exception{

        // 1. 生成Http对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2. 获取httpGet对象
        HttpGet httpGet = new HttpGet("http://www.tuicool.com/");
        // 3. 执行httpGet
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 4. 获取返回的实体
        HttpEntity httpEntity = response.getEntity();
        // 5. 解析实体类
        String entityJson = EntityUtils.toString(httpEntity,"utf-8");
        // 6. 打印数据,观看结果
        System.out.println("返回的数据是:" + entityJson);
        // 7. 关闭连接对象
        response.close();
        httpClient.close();
    }
}

网页内容:

<head>

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>

<body>

    <p>系统检测亲不是真人行为,因系统资源限制,我们只能拒绝你的请求。如果你有疑问,可以通过微博 http://weibo.com/tuicool2012/ 联系我们。</p>

</body>

我们模拟下浏览器 设置下User-Agent头消息:

加下 httpGet.setHeader(“User-Agent”, “Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0”); // 设置请求头消息User-Agent



import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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;

/**
 * @author Zerox
 * @date 2019/2/21 8:38
 */
public class Demo1Test {

    /**
     *  1. 第一次执行之后检测到没有请求头~
     */
    public static void main(String[] args) throws Exception{

        // 1. 生成Http对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2. 获取httpGet对象
        HttpGet httpGet = new HttpGet("http://www.tuicool.com/");
        // 2.1 设置http请求头 User-Agent
        httpGet.setHeader("User-Agent","Mozilla/5.0(Windows NT 6.1;Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
        // 3. 执行httpGet
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 4. 获取返回的实体
        HttpEntity httpEntity = response.getEntity();
        // 5. 解析实体类
        String entityJson = EntityUtils.toString(httpEntity,"utf-8");
        // 6. 打印数据,观看结果
        System.out.println("返回的数据是:" + entityJson);
        // 7. 关闭连接对象
        response.close();
        httpClient.close();
    }
}

都是可以通过setHeader方法 设置key value;来得到模拟浏览器请求;

代码地址:https://github.com/xueh0430/http-User-Agent.git (IDEA管理工具)

该文章为转载文章,原文章地址为:https://blog.csdn.net/u013215018/article/details/55045392#comments 大家可以关注下

发布了32 篇原创文章 · 获赞 26 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_27416233/article/details/87856776
今日推荐