【问题标题】:Rewrite HAR content via BrowserMobProxy and Selenium通过 BrowserMobProxy 和 Selenium 重写 HAR 内容
【发布时间】:2016-05-23 14:28:08
【问题描述】:

是否可以更改来自 https 站点的 har(其中包含 json)响应?我通过getText() 看到初始json,然后使用setText(),并通过setSizesetBodySize 更改其大小。正如我在调试中看到的那样,内容已更改。但似乎没有任何效果,chrome收到相同的旧json。有什么办法吗?

    public static class ResponseModifier implements ResponseInterceptor {

    @Override
    public void process(BrowserMobHttpResponse response, Har har) {

        String contentType = response.getHeader("Content-Type");

        if (contentType!=null && contentType.startsWith("application/json")){
            response.getEntry().getResponse().getContent().setText(respond);
            response.getEntry().getResponse().getContent().setSize(respond.length());
            response.getEntry().getResponse().setBodySize(respond.length());
            response.getRawResponse().removeHeaders("Content-Length");
            response.getRawResponse().addHeader("Content-Length", ""+respond.length());
        }
    }

我认为也许问题在于response.getRawResponse().getEntity().getContentLength() 以某种方式具有旧值,但我无法重写它。还是原因不同?

【问题讨论】:

  • 你不是在重写一个 HAR,而是一个 HTTP 响应。 HAR 是请求 + 响应的 JSON 描述的导出存档。有点困惑为什么你会通过 HTTP 请求一个(然后尝试摆弄响应)而不是从代理服务器以编程方式。
  • 我需要用 json 更改 http 响应,所以我认为 har 是这样。如何以其他方式更改响应内容?
  • ResponseInterceptor 是正确的方法,但我认为术语令人困惑 - API 似乎也在不断变化。等等,这是你吗? github.com/lightbody/browsermob-proxy/issues/382
  • 不确定我能提供更多帮助。我停止使用 BMP 进行开发,并等待听到 2.1 发布以及 API 问题完全解决的消息。你现在或许可以更好地回答这个问题了。

标签: json http har browsermob-proxy


【解决方案1】:

结果比我想象的要容易得多。 我必须使用 BMP 的 core-littleproxy 版本,BrowserMobProxy 类并创建 BrowserMobProxyServer 的实例。我只是使用过滤器:

 proxy.addResponseFilter((response, contents, messageInfo) -> {
        if ((contents.getContentType().startsWith("application/json"))) {
            contents.setTextContents(respond);
        }
    }); 

感谢阅读:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-12
    • 2018-03-08
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多