【问题标题】:get Har data in json and string format获取 json 和字符串格式的 Har 数据
【发布时间】:2016-10-10 09:08:08
【问题描述】:

我已经编写了一个类,用于从 firefox 浏览器获取 har 数据。

我想正确显示 JSON 格式的 har 数据。

我的代码:

ProxyServer server = new ProxyServer(4444);
    server.start();

    //captures the moouse movements and navigations
    server.setCaptureHeaders(true);
    server.setCaptureContent(true);

    // get the Selenium proxy object
    Proxy proxy = server.seleniumProxy();

    // configure it as a desired capability
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, proxy);

    // start the browser up
    WebDriver driver = new FirefoxDriver(capabilities);

    // create a new HAR with the label "apple.com"
    server.newHar("yahoo.com");

    // open yahoo.com
    driver.get("http://yahoo.com");

    // get the HAR data
    net.lightbody.bmp.core.har.Har har = server.getHar();

谁能帮我获取 JSON 格式和字符串的 HAR 数据!

【问题讨论】:

    标签: java selenium-webdriver har browsermob-proxy


    【解决方案1】:

    抱歉回复晚了。

    希望它可以帮助这里的其他人。

    您可以使用 StringWriter 将 Har 写入 String。所以你可以得到 har 作为字符串。而且,您可以使用 Gson 解析该字符串以获取 JSON 对象。

    net.lightbody.bmp.core.har.Har har = server.getHar();
    
    java.io.StringWriter writer = new java.io.StringWriter();
    try {
                har.writeTo(writer);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    String harAsString = writer.toString();
    
    com.google.gson.JsonElement harAsJson = new com.google.gson.Gson().toJsonTree(writer.toString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多