使用 BrowserMob-Proxy REST api 设置自定义标头 [英] Setting a custom header using BrowserMob-Proxy REST api

查看:20
本文介绍了使用 BrowserMob-Proxy REST api 设置自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 9091 端口上运行的 browsermob 代理.我正在尝试使用 browsermob-proxy REST API 来设置自定义标头.当我通过代理使用 Selenium 向我的应用程序发出请求时,我没有看到我的应用程序控制台中打印的标题.下面是我的代码.请求正文基于此处的文档.我的要求是在这个特定用例中使用 BrowserMob 代理 API 而不是它的 Java 库.我在下面的代码中做错了什么?

I have a browsermob proxy running on port 9091. I am trying to use browsermob-proxy REST API to set a custom header. When I make a request to my app using Selenium via the proxy, I don't see the header printed in my apps console. Below is my code. The request body is based on documentation here. My requirement is to use BrowserMob proxy API and not its Java library for this particular use case. Anything I am doing wrong in the code below?

 Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress("localhost", 9091));

 String bpmUrl = "http://localhost:8787/proxy/9091/interceptor/request";
 Client client = Client.create();
 String requestBody = "request.getMethod().addHeader("custom-header", "Bananabot/1.0");";
 WebResource resource = client.resource(bpmUrl);
 resource.type(MediaType.TEXT_PLAIN_TYPE).post(requestBody);

 String url = "http://localhost:8004";
 DesiredCapabilities capabilities = DesiredCapabilities.firefox();
 capabilities.setCapability(CapabilityType.PROXY, proxy);
 WebDriver driver = new FirefoxDriver(capabilities);
 driver.get(url);
 driver.quit();

编辑 1

我尝试了@Erki 的解决方案,我认为它应该有效,但它无效.这里有什么遗漏吗?

I tried @Erki's solution which I think should work, but its not. Anything missing here?

 String bpmUrl = "http://localhost:8787/proxy/9091/headers";
     Map<String,String> data = new HashMap<String, String>();
     data.put("user-agent","Bananabot");
     ClientConfig cc = new DefaultClientConfig();
     cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
     Client client = Client.create(cc);
     WebResource resource = client.resource(bpmUrl);
     resource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, data);

编辑 2找到了答案,我尝试了 Java API,它工作正常,并注意到浏览器正在按预期使用代理.使用上面的代码,显然浏览器没有使用代理.因此,我没有使用 java.net.Proxy,而是使用了有效的 org.openqa.selenium.Proxy.所以我需要的唯一代码更改是我初始化代理的方式,其余部分相同.这现在工作正常.

Edit 2 Found the answer, I tried Java API, it worked and noticed that the browser is using proxy as expected. With my code above, obviously the browser was not using proxy. So instead of using java.net.Proxy I used org.openqa.selenium.Proxy which worked. So the only code change I needed was the way I initialize the proxy, rest of it is same. This works fine now.

 String PROXY = "localhost:9091";
 Proxy proxy = new Proxy();
 proxy.setHttpProxy(PROXY);

推荐答案

您使用的代码对应于在嵌入模式下使用 BMP:

You are using the code that corresponds to using the BMP in embedded mode:

server.addRequestInterceptor(new RequestInterceptor() {
    @Override
    public void process(BrowserMobHttpRequest request, Har har) {
        request.getMethod().removeHeaders("User-Agent");
        request.getMethod().addHeader("User-Agent", "Bananabot/1.0");
    }
});

如果您实际上以嵌入式模式启动了代理服务器,则此代码会起作用,据我所知,这不是您已经完成或打算执行的操作.您可能需要的是:

This code would do if you had actually started the proxy server in embedded mode, which, as far as I understand, is not what you have done or have intended to. What you need is probably:

POST/proxy/[port]/headers - 设置和覆盖 HTTP 请求标头.例如设置自定义用户代理.负载数据应该是json编码的标头集(不是 url 编码的)

POST /proxy/[port]/headers - Set and override HTTP Request headers. For example setting a custom User-Agent. Payload data should be json encoded set of headers (not url-encoded)

这篇关于使用 BrowserMob-Proxy REST api 设置自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆