java抓包方法

很多网站都会对抓包进行处理,我这里所做的抓包模式是模拟网页请求进行数据抓取

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进行数据转换,简单方便好用。

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值