2016-06-09 59 views
1

我写过一个从firefox浏览器获取har数据的类。以json和字符串格式获取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数据!

回答

1

对不起,对于迟到的答案。

希望它可以帮助别人在这里。

您可以使用StringWriter将Har写入字符串。所以你可以像弦一样得到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());