0

有人熟悉在 Java 中使用HarLib吗?我正在尝试从 .HAR 文件中获取“响应”数据并将其输出到屏幕。

类中有一个getResponse()方法HarEntry,但是我不确定如何根据主页上的示例来实现它:

来自网站的样本:

import edu.umass.cs.benchlab.har.*;

File f = new File(fileName);
HarFileReader r = new HarFileReader();
HarFileWriter w = new HarFileWriter();
try
{
  System.out.println("Reading " + fileName);
  HarLog log = r.readHarFile(f);

  // Access all elements as objects
  HarBrowser browser = log.getBrowser();
  HarEntries entries = log.getEntries();
  List<HarPage> pages = log.getPages().getPages();
  for (HarPage page : pages)
  {
    System.out.println("page start time: "
        + ISO8601DateFormatter.format(page.getStartedDateTime()));
    System.out.println("page id: " + page.getId());
    System.out.println("page title: "+page.getTitle());
  }

           // Once you are done manipulating the objects, write back to a file
  System.out.println("Writing " + fileName + ".test");
  File f2 = new File(fileName + ".test");
  w.writeHarFile(log, f2);
}
catch (JsonParseException e)
{
  e.printStackTrace();
  fail("Parsing error during test");
}
catch (IOException e)
{
  e.printStackTrace();
  fail("IO exception during test");
}
4

1 回答 1

0

我想我想通了,虽然我不知道这段代码有多“干净”。我修改了示例代码并注释掉了写入文件部分:

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.JsonParseException;

import edu.umass.cs.benchlab.har.*;
import edu.umass.cs.benchlab.har.tools.*;


public class readinghars {


public static void main(String[] args) {
    String filename = new String("filename.har");
    File f = new File(filename);
    HarFileReader r = new HarFileReader();
    HarFileWriter w = new HarFileWriter();

    try
    {
      System.out.println("Reading " + filename);
      HarLog log = r.readHarFile(f);

      // Access all elements as objects
      HarBrowser browser = log.getBrowser();
      HarEntries entries = log.getEntries();

   // Used for loops
      List<HarPage> pages = log.getPages().getPages();
      List<HarEntry> hentry = entries.getEntries(); 

      for (HarPage page : pages)
      {
        System.out.println("page start time: "
            + ISO8601DateFormatter.format(page.getStartedDateTime()));
        System.out.println("page id: " + page.getId());
        System.out.println("page title: " + page.getTitle());
        }

      //Output "response" code of entries.
      for (HarEntry entry : hentry)
      {
          System.out.println("request code: " + entry.getRequest().getMethod()); //Output request type
          System.out.println("    response code: " + entry.getRequest().getUrl()); //Output url of request
          System.out.println("    response code: " + entry.getResponse().getStatus()); // Output the 
      }

      /*
      // Once you are done manipulating the objects, write back to a file
      System.out.println("Writing " + "fileName" + ".test");
      File f2 = new File("fileName" + ".test");
      w.writeHarFile(log, f2);
      */
    }
    catch (JsonParseException e)
    {
      e.printStackTrace();
      //fail("Parsing error during test");
    }
    catch (IOException e)
    {
      e.printStackTrace();
      //fail("IO exception during test");
    }

}

}

于 2012-07-31T13:32:02.243 回答