Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

java - Setting a custom header using BrowserMob-Proxy REST api

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();

Edit 1

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);

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 - 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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...