很多网站都会对抓包进行处理,我这里所做的抓包模式是模拟网页请求进行数据抓取
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
public class HttpUtil {
public static String sendGet(String url, Map<String, String> headers) throws Exception {
URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
// 设置请求方法为GET
connection.setRequestMethod("GET");
// 添加请求头
addHeaders(connection, headers);
// 获取响应码
int responseCode = connection.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
public static String sendPost(String url, String body, Map<String, String> headers) throws Exception {
URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
// 设置请求方法为POST
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// 添加请求头
addHeaders(connection, headers);
// 发送请求体
try (OutputStream os = connection.getOutputStream()) {
byte[] input = body.getBytes("utf-8");
os.write(input, 0, input.length);
}
// 获取响应码
int responseCode = connection.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
private static void addHeaders(HttpURLConnection connection, Map<String, String> headers) {
// 添加请求头
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
connection.setRequestProperty(header.getKey(), header.getValue());
}
}
}
public static void main(String[] args) {
try {
// GET请求示例
String url = "https://jsonplaceholder.typicode.com/posts";
headers.put("User-Agent", "Mozilla/5.0");
headers.put("Date", new Date().toString());
headers.put("Content-Type", "application/json");
headers.put("Transfer-Encoding", "chunked");
headers.put("Connection", "keep-alive");
headers.put("vary", "Accept-Encoding");
headers.put("request-id", "1234567890");
headers.put("x-request-id", "0987654321");
headers.put("Content-Encoding", "gzip");
headers.put("CF-Cache-Status", "HIT");
headers.put("Server", "cloudflare");
headers.put("CF-RAY", "abc123def456ghi789");
headers.put("alt-svc", "h3-23=:443");
String response = sendGet(url, headers);
System.out.println("GET Response: " + response);
// POST请求示例
url = "https://jsonplaceholder.typicode.com/posts";
String body = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
response = sendPost(url, body, headers);
System.out.println("POST Response: " + response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当然此方法也可以作为正常的httputils方法去使用,只不过核心在于自定义请求头,此处推荐fastjson进行数据转换,简单方便好用。