Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java上的browsermob代理中添加自定义cookie_Java_Cookies_Selenium Webdriver_Proxy_Browsermob - Fatal编程技术网

如何在java上的browsermob代理中添加自定义cookie

如何在java上的browsermob代理中添加自定义cookie,java,cookies,selenium-webdriver,proxy,browsermob,Java,Cookies,Selenium Webdriver,Proxy,Browsermob,我正在编写自动化测试,为了捕获在后台进行的网络调用,我正在使用browsermob代理 在browsermob代理中,我想在发出请求之前设置cookie。我怎么做 下面是我的代码:- String strFilePath = "data.har"; // start the proxy ProxyServer server = new ProxyServer(4444); server.start(); server.setCa

我正在编写自动化测试,为了捕获在后台进行的网络调用,我正在使用browsermob代理

在browsermob代理中,我想在发出请求之前设置cookie。我怎么做

下面是我的代码:-

String strFilePath = "data.har";

        // start the proxy
        ProxyServer server = new ProxyServer(4444);
        server.start();

        server.setCaptureHeaders(true);
        server.setCaptureContent(true);

        // get the Selenium proxy object
        Proxy proxy = server.seleniumProxy();

        FirefoxProfile profile = new FirefoxProfile();
        String userAgent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 MobileWeb/8H7 Safari/6533.18.5";
        profile.setPreference("general.useragent.override", userAgent);

        // configure it as a desired capability
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability(CapabilityType.PROXY, proxy);
        capabilities.setCapability(FirefoxDriver.PROFILE, profile);

        // start the browser up
        WebDriver driver = new FirefoxDriver(capabilities);

        final String[] remoteHost = {null};
        final String[] analytics = {null};
        final String[] fetchAdjs = {null};


        server.addRequestInterceptor(new RequestInterceptor()
        {
            int googleCount = 0;
            int adjs = 0;

            @Override
            public void process(BrowserMobHttpRequest browserMobHttpRequest, Har har)
            {
                remoteHost[0] = browserMobHttpRequest.getProxyRequest().getRemoteHost();

                String request = browserMobHttpRequest.getProxyRequest().getRequestURL().toString();

                if (request.matches(".*google.*"))
                    googleCount = googleCount + 1;

                if (request.matches(".*test.*"))
                    adjs = adjs + 1;

                analytics[0] = String.valueOf(googleCount);
                fetchAdjs[0] = String.valueOf(adjs);

                // System.out.println(browserMobHttpRequest.getMethod().getAllHeaders()[1]);  // user agent

                System.out.println(browserMobHttpRequest.getProxyRequest());
            }
        });

        // create a new HAR with the label "apple.com"
        server.newHar("assertselenium.com");

        // open yahoo.com
        driver.get("http://test.com");

        Thread.sleep(3000);
        Thread.sleep(3000);

        driver.get("http://test.com/316782/content/fDxL4zzv");
        Thread.sleep(3000);

        // get the HAR data
        Har har = server.getHar();

        FileOutputStream fos = new FileOutputStream(strFilePath);

        // view har file here --> http://pcapperf.appspot.com/
        har.writeTo(fos);
        server.stop();
        driver.quit();

使用请求过滤器是正确的方法。但是,我强烈建议使用LittleProxy集成构建最新版本的BrowserMob代理。它的过滤器更可靠,更容易使用。有关构建和使用最新版本的信息,请参见。本节将特别相关

下面是一个使用新请求筛选器向每个请求添加cookie的简单示例:

    BrowserMobProxy proxy = new BrowserMobProxyServer();
    proxy.start();

    proxy.addRequestFilter(new RequestFilter() {
        @Override
        public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpRequest originalRequest) {
            request.headers().add("Cookie", "added-cookie=added-value");

            return null;
        }
    });

创建浏览器配置文件-请参阅此答案-。添加
user dir
选项将使用浏览器配置文件。您好,我只是想问一下。为什么返回空值?它会影响HttpResponse结果吗?@AldoSuwandi:返回null只意味着“继续常规处理,我不想返回短路响应”。它不会影响服务器的响应。