Lisy's blog Lisy's blog
首页
后端
前端
读书
关于

新晴

做事情积极主动。积极主动不仅指行事的态度,还意味着为人一定要对自己的人生负责。个人行为取决于自身的抉择,而不是外在的环境,人类应该有营造有利的外在环境的积极性和责任感。
首页
后端
前端
读书
关于
  • Java

    • Java总览
    • 日常总结

      • hutool常用日期功能
      • 基于hutool请求工具使用代理
        • 一、hutool maven
        • 二、使用代理请求
        • 三 hutool http工具常用功能
          • (一)下载文件:
          • (二)常用请求
          • (三)获取客户端ip
      • Apache JDBC工具(commons-dbutils)
      • jdbcTemplate(NamedParameterJdbcTemplate )使用
      • spring boot RestTemplate get设置header
      • git常用命令
      • 使用阿里seata处理分布式事务(nacos作为注册中心)
      • java诊断工具Arthas使用
      • 利用jsoup获取网页内容,然后对内容进行分析
      • mysql允许远程访问
      • java部署速查
    • 效率工具

  • linux

  • python

  • 其他

  • python

  • 后端
  • Java
  • 日常总结
lisy
2020-04-19
目录

基于hutool请求工具使用代理

# 一、hutool maven

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>
1
2
3
4
5

hutool官网 (opens new window)

# 二、使用代理请求

package cn.lisynet.tools;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpDo {
    public static void main(String[] args) {
        Map<String, List<String>> headers = new HashMap<>();

        headers.put("Host",new ArrayList<String>(){{add("www.baidu.com");}});
        headers.put("X- Forwarded-For",new ArrayList<String>(){{add("1.65.202.188");}});
        headers.put("HTTP_CLIENT_IP",new ArrayList<String>(){{add("1.65.202.188");}});
        headers.put("HTTP_X_FORWARDED_FOR",new ArrayList<String>(){{add("1.65.202.188");}});
        headers.put("User-Agent",new ArrayList<String>(){{add("hutool");}});
        headers.put("Accept",new ArrayList<String>(){{add("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");}});
        headers.put("Accept-Language",new ArrayList<String>(){{add("zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");}});
        headers.put("DNT",new ArrayList<String>(){{add("1");}});
        headers.put("Connection",new ArrayList<String>(){{add("Connection");}});
        headers.put("Cookie",new ArrayList<String>(){{add("Hm_lvt_d430ac74b98d0b8bc5d3a2c4643a9405=1599994032; Hm_lpvt_d430ac74b98d0b8bc5d3a2c4643a9405=1599994032; Hm_lvt_26f8160d01d267d3535270f11632d47b=1599994032; Hm_lpvt_26f8160d01d267d3535270f11632d47b=1599994039");}});
        headers.put("Upgrade-Insecure-Requests",new ArrayList<String>(){{add("1");}});

        Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("103.28.37.131", 3128));
        HttpResponse rs = HttpRequest.get("https://www.baidu.com")
                .header(headers,true)
                .timeout(120000)
                .setProxy(proxy)
                .execute();
        System.out.println(rs.body());
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# 三 hutool http工具常用功能

# (一)下载文件:

String audioFileUrl = "https://test.demo.com/123.mp3";
InputStream inputStream = HttpRequest.get(audioFileUrl).execute().bodyStream();
String rsPath = "D:/123.mp3";
FileUtil.writeFromStream(inputStream, rsPath);
1
2
3
4

# (二)常用请求

String url = "http://localhost:8080/user/login";
String params = "{\"username\":\"admin\",\"password\":\"123456\"}";
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type","application/json");

HttpResponse response = HttpRequest.post(url)
                .headerMap(headers,true)
                .timeout(3000)
                .body(params)
                .execute();
System.out.println(response.body());
1
2
3
4
5
6
7
8
9
10
11

# (三)获取客户端ip

默认检测的Header:1、X-Forwarded-For 2、X-Real-IP 3、Proxy-Client-IP 4、WL-Proxy-Client-IP

// request即javax.servlet.http.HttpServletRequest
String ip = ServletUtil.getClientIP(request, null);
System.out.println(ip);
1
2
3
#Java
上次更新: 2024/01/07, 07:44:52
hutool常用日期功能
Apache JDBC工具(commons-dbutils)

← hutool常用日期功能 Apache JDBC工具(commons-dbutils)→

最近更新
01
Anaconda conda命令速查
01-05
02
奇特的一生
06-16
03
docker学习
02-16
更多文章>
Theme by Vdoing | Copyright © 2020-2025 lisy | 备案号:鲁ICP备18023702号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式