基于hutool请求工具使用代理
# 一、hutool maven
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 二、使用代理请求
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
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
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
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
2
3
上次更新: 2024/01/07, 07:44:52