【问题标题】:How to read cookie from org.eclipse.swt.browser.Browser?如何从 org.eclipse.swt.browser.Browser 读取 cookie?
【发布时间】:2016-03-19 06:14:10
【问题描述】:

我想从 cookie org.eclipse.swt.browser.Browser 中读取 JSESSIONID。我尝试从 Eclipse 插件打开浏览器。 我在下面使用sn-p

public static void main(String[] args)
{
    Display display = new Display();

    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout());

    final Browser browser = new Browser(shell, SWT.NONE);

    final String url = "https://....";
    browser.setUrl(url);
    browser.addProgressListener(new ProgressAdapter() {
        @Override
        public void completed(ProgressEvent event) {
            String cookieText = "cookie=" + Browser.getCookie("JSESSIONID", url);
            System.out.println(cookieText);
        }
    });
    shell.setSize(400, 300);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }

    display.dispose();
}

但我没有得到 cookie 值。

类似这样的东西:c# Get httponly cookie

【问题讨论】:

  • 我用http://stackoverflow.com/ 试过你的sn-p。在此之前,我使用 Chromes 检查器查看实际发送了哪些 cookie(总共 5 个)。但显然 SWT 无法看到所有这些。找不到的是prov,2055年到期,能看到的最晚2018年到期。也许你的 JSESSIONID 有一个类比?

标签: java cookies browser swt


【解决方案1】:

尝试从 JavaScript 而不是 Browser#getCookie() 方法获取 cookie。在我的测试过程中它对我有用,但由于我不知道你的网站,我无法对其进行测试:

public static void main(String[] args)
{
    Display display = new Display();

    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout());

    final Browser browser = new Browser(shell, SWT.NONE);
    browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    final String url = "https://...";
    browser.setUrl(url);

    /* Define the function to call from JavaScript */
    new BrowserFunction(browser, "cookieCallback") {
        @Override
        public Object function(Object[] objects) {

            Object[] keyValuePairs = (Object[]) objects[0];

            for(Object keyValue : keyValuePairs)
            {
                Object[] pair = (Object[]) keyValue;

                if(Objects.equals("JSESSIONID", pair[0]))
                    System.out.println(pair[1]);
            }

            return null;
        }
    };

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Get cookie");
    button.addListener(SWT.Selection, new Listener() {
        @Override
        public void handleEvent(Event event) {
            /* Get the cookie from JavaScript and then call the function */
            browser.execute("cookieCallback(document.cookie.split( ';' ).map( function( x ) { return x.trim().split( '=' ); } ));");
        }
    });

    shell.setSize(400, 300);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }

    display.dispose();
}

【讨论】:

  • 我没有得到 JSESSIONID,而是从 cookie 中获取其他详细信息,例如 cookie=true 等。
  • @happy 如果你只是在循环中打印cookie,它不包含cookie?你能在实际的浏览器中验证设置了 cookie 吗?
  • 在实际浏览器中我可以看到 JSESSIONID=...,cookie=true,NSC_TMAA=..,NSC_TMAS=..;当我调试代码时,我只得到 cookie=true,NSC_TMAA=..,NSC_TMAS=..; .我猜是httponly设置为true的问题。
  • @happy 很有可能,是的。
  • @happy 你能把httponly 设置为false 作为测试吗?
【解决方案2】:

如果您希望获取的 cookie 标记为 httpOnly,那么您将无法在当前 SWT 版本中获取它。请参阅this bug 进行讨论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2015-01-30
    相关资源
    最近更新 更多