Hello,
The requirement:
- Use Chrome Web browser on Android (real device or an emulator)
- Proxy HTTP and HTTPS traffic to add request headers and read response headers
I am looking for an embedded proxy so that I programatically read and add headers.
I have been looking into BrowserMob Proxy (browsermob-core-littleproxy 2.1.0-beta-3) and while I was able to proxy HTTP traffic, I am unable to proxy HTTPS traffic!
When I try to proxy an HTTPS site, the Chrome browser tries to load the page but it fails after 30 seconds or so
Is there a certificate that I need to to install on the Android device? in order for this to work? Does anyone know what I exactly need to do so that I can proxy HTTPS traffic using BrowserMob Proxy?
But if I try do the same while setting FULL_URL to an HTTPS site, like "
http://www.google.com/", BrowerMob Proxy does not work as expected
public class LittleProxyExampleMobile {
private final static Logger LOGGER = LoggerFactory.getLogger(LittleProxyExampleMobile.class);
private BrowserMobProxy proxy;
private RemoteWebDriver driver;
private final static String FULL_URL = "http://www.urbandictionary.com/";
// private final static String FULL_URL = "http://www.google.com/";
@BeforeSuite
public void setup() {
proxy = new BrowserMobProxyServer(9090);
// configure it as a desired capability
DesiredCapabilities caps = DesiredCapabilities.android();
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.2");
caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 0);
caps.setCapability(MobileCapabilityType.ACCEPT_SSL_CERTS, true);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("ignore-certificate-errors");
caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
try {
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Failed to init RemoveWebDriver");
}
addProxyListenners();
}
@Test
public void test() {
// create a new HAR
proxy.newHar(FULL_URL);
driver.get(FULL_URL);
// get the HAR data
Har har = proxy.getHar();
}
@AfterSuite
public void tearDown() {
proxy.stop();
driver.close();
driver.quit();
}
private void addProxyListenners() {
proxy.addRequestFilter(new RequestFilter() {
@Override
public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpMessageInfo messageInfo) {
LOGGER.info("HttpRequest received with messageInfo " + messageInfo.getOriginalUrl());
if (messageInfo.getOriginalUrl().endsWith(FULL_URL)) {
request.headers().add("key", "value");
LOGGER.info("Header added for URL: " + messageInfo.getOriginalUrl());
}
return null;
}
});
proxy.addResponseFilter(new ResponseFilter() {
@Override
public void filterResponse(HttpResponse response, HttpMessageContents contents, HttpMessageInfo messageInfo) {
LOGGER.info("HttpResponse received with messageInfo " + messageInfo.getOriginalUrl());
if (messageInfo.getOriginalUrl().endsWith(FULL_URL)) {
HttpHeaders httpHeaders = response.headers();
for (Map.Entry<String, String> entry : httpHeaders.entries()) {
LOGGER.info(entry.getKey() + ": " + entry.getValue());
}
}
}
});
}
}
Thanks