browsermob-proxy

最后更新于:2022-04-01 09:43:28

> 参考文章: [browsermob-proxy](https://github.com/lightbody/browsermob-proxy#new-browsermobproxy-api) [lightbody](http://bmp.lightbody.net/) [selenium](http://wenku.baidu.com/link?url=WbKRY4nmP8VQUkwn8sKRYm97yfFab2aOlkgPFzOKo6-XvDfum3uwPXqpcXzqPHsPq2tPvwbTdvuYAfRpakrhRmRqmVL4pa8dW8bEnoHmouS) ## browsermob-proxy是什么 提供命令行的方式或者嵌入到Java项目中通过api方式生成HAR文件的插件,属于selenium的一个子项目。具体的流程有点类似与Flidder或Charles。原理开启一个端口监控某个端口下的所有HTTP请求,进行数据抓取。 ## Java API ### 依赖 ~~~ compile 'net.lightbody.bmp:browsermob-core-littleproxy:2.1.0-beta-4' compile 'org.seleniumhq.selenium:selenium-java:2.49.0' ~~~ ### 代码 ~~~ BrowserMobProxy proxy = new BrowserMobProxyServer(); proxy.start(); Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.PROXY, seleniumProxy); WebDriver driver = new FirefoxDriver(capabilities); proxy.newHar("http://www.baidu.com"); driver.get("http://www.baidu.com"); Har har = proxy.getHar(); har.writeTo(new File("baidu.com.har")); driver.close(); ~~~ ![这里写图片描述](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-23_56cbdb0d238b2.jpg "") 运行过程如下: ![这里写图片描述](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-23_56cbdb0d3e319.jpg "") ## 命令行 ![这里写图片描述](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-23_56cbdb0f53b5b.jpg "") 下载release版本后,解压,进入该目录bin目录下,执行命令`sh browsermob-proxy`输出如下信息: ~~~ 58deMacBook-Pro-7:bin wuxian$ sh browsermob-proxy Running BrowserMob Proxy using LittleProxy implementation. To revert to the legacy implementation, run the proxy with the command-line option '--use-littleproxy false'. [INFO 2016-01-14T19:23:50,254 net.lightbody.bmp.proxy.Main] (main) Starting BrowserMob Proxy version 2.1.0-beta-4 [INFO 2016-01-14T19:23:51,510 org.eclipse.jetty.util.log] (main) jetty-7.x.y-SNAPSHOT [INFO 2016-01-14T19:23:51,546 org.eclipse.jetty.util.log] (main) started o.e.j.s.ServletContextHandler{/,null} [INFO 2016-01-14T19:23:52,463 org.eclipse.jetty.util.log] (main) Started SelectChannelConnector@auto.58corp.com:8080 ~~~
';