有什么方法可以使用Selenium chrome驱动程序为header x-forwarded-for设置自定义值?

时间:2018-08-23 06:47:02

标签: java selenium selenium-chromedriver

我正在使用Selenium chrome驱动程序将请求发送到我自己的服务器,在服务器端,我已打印收到的所有请求标头。

在标题列表中,我收到了标题x-forwarded-for = unknown

我的servlet中的代码是:

public class QueryValidatorServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<String> headers = Collections.list(req.getHeaderNames());
        for (int count = 0; count < headers.size(); count++) {
            String headerName = headers.get(count);
            System.out.println("Header  - " + count + " : " + headerName + " = " + req.getHeader(headerName));
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       ArrayList<String> headers = Collections.list(req.getHeaderNames());
       for (int count = 0; count < headers.size(); count++) {
            String headerName = headers.get(count);
            System.out.println("Header  - " + count + " : " + headerName + " = " + req.getHeader(headerName));
       }
    }

}

我用来发送请求的代码如下:

String PROXY = <proxy ip> + ":" + <proxy port>;     
Proxy proxy = new Proxy();
proxy.setHttpProxy(PROXY);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.PROXY, proxy);

ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
options.merge(capabilities);

ChromeDriver webDriver = new ChromeDriver(options);
webdriver.get("http://<server ip>:<server port>/myApp/validateQuery")

有什么方法可以设置此标头,即将x-forwarded-for设置为自定义值,而不是通过Java代码未知吗?

0 个答案:

没有答案