You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Chrome DevTools中的HTTP请求保存到本地或通过Java代码访问?

Hey there! Great question—saving and accessing those Chrome DevTools Network requests is totally feasible, whether you want to store them locally manually or pull them into your Java code. Let me walk you through both approaches:

1. Manually Save Network Requests to a Local File

This is super straightforward for one-off captures:

  • Open Chrome DevTools (hit F12 or Ctrl+Shift+I on Windows/Linux, Cmd+Opt+I on Mac), then switch to the Network tab.
  • Trigger the actions that generate the requests you want to capture (like refreshing the page or interacting with your app).
  • Right-click on a single request (or select multiple requests, then right-click) and choose Save all as HAR with content.
  • Pick a local folder to save the file—you’ll get a .har file, which is a JSON-formatted document containing every detail of the requests and responses (URLs, headers, status codes, response bodies, etc.). You can open it with any text editor or even dedicated HAR viewers.
2. Access Network Requests via Java Code

You’ve got two main options here: parsing a pre-saved HAR file, or capturing requests in real-time using a proxy library.

Option A: Parse a Local HAR File

Since HAR files are just structured JSON, you can use popular Java JSON libraries like Jackson or Gson to read and extract data. Here’s a quick example with Jackson:

First, add the Jackson dependency to your pom.xml (if using Maven):

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>

Then, create a simple parser class to map the HAR structure to Java objects:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class HarRequestParser {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // Load the HAR file
            HarFile harFile = objectMapper.readValue(new File("your-saved-requests.har"), HarFile.class);
            List<HarEntry> requestEntries = harFile.getLog().getEntries();

            // Iterate through each captured request
            for (HarEntry entry : requestEntries) {
                System.out.println("Request URL: " + entry.getRequest().getUrl());
                System.out.println("HTTP Method: " + entry.getRequest().getMethod());
                System.out.println("Response Status Code: " + entry.getResponse().getStatus());
                // You can add more fields like headers, request body, response body here
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // POJO classes to map the HAR JSON structure
    static class HarFile {
        private HarLog log;
        public HarLog getLog() { return log; }
        public void setLog(HarLog log) { this.log = log; }
    }

    static class HarLog {
        private List<HarEntry> entries;
        public List<HarEntry> getEntries() { return entries; }
        public void setEntries(List<HarEntry> entries) { this.entries = entries; }
    }

    static class HarEntry {
        private HarRequest request;
        private HarResponse response;
        public HarRequest getRequest() { return request; }
        public void setRequest(HarRequest request) { this.request = request; }
        public HarResponse getResponse() { return response; }
        public void setResponse(HarResponse response) { this.response = response; }
    }

    static class HarRequest {
        private String url;
        private String method;
        // Add fields like headers, body if needed
        public String getUrl() { return url; }
        public void setUrl(String url) { this.url = url; }
        public String getMethod() { return method; }
        public void setMethod(String method) { this.method = method; }
    }

    static class HarResponse {
        private int status;
        // Add fields like statusText, headers if needed
        public int getStatus() { return status; }
        public void setStatus(int status) { this.status = status; }
    }
}

Option B: Capture Requests in Real-Time

If you want to capture requests as they happen (instead of using a pre-saved HAR), you can use a proxy library like BrowserMob Proxy. It acts as a middleman between Chrome and the server, letting you intercept requests programmatically.

Add the BrowserMob Proxy dependency to your pom.xml:

<dependency>
    <groupId>net.lightbody.bmp</groupId>
    <artifactId>browsermob-core</artifactId>
    <version>2.1.5</version>
</dependency>

Here’s a sample to start capturing requests:

import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.core.har.HarEntry;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class RealTimeRequestCapture {
    public static void main(String[] args) {
        // Start the proxy server on port 8080
        BrowserMobProxy proxy = new BrowserMobProxyServer();
        proxy.start(8080);

        // Initialize a HAR capture
        proxy.newHar("my-app-requests");

        // Now configure Chrome to use this proxy:
        // Go to Chrome settings > System > Open your computer's proxy settings,
        // or start Chrome with the flag: `chrome.exe --proxy-server=localhost:8080`

        // Trigger your app's actions (manually or via Selenium for automation)

        // Retrieve captured requests
        Har capturedHar = proxy.getHar();
        List<HarEntry> entries = capturedHar.getLog().getEntries();

        for (HarEntry entry : entries) {
            System.out.println("Captured URL: " + entry.getRequest().getUrl());
            System.out.println("Response Status: " + entry.getResponse().getStatus());
        }

        // Save the captured HAR to a file
        try {
            capturedHar.writeTo(new File("real-time-captures.har"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Stop the proxy when done
        proxy.stop();
    }
}

Pro tip: If you want to automate browser actions alongside capturing requests, pair this with Selenium WebDriver—you can set ChromeOptions to use the proxy directly in your code.

内容的提问来源于stack exchange,提问作者Alexis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:49:59