如何从org.eclipse.swt.browser.Browser读取cookie?

hap*_*ppy 7 java browser cookies swt

我想JSESSIONID从cookie中读取org.eclipse.swt.browser.Browser.我尝试从Eclipse插件打开浏览器.我正在使用下面的代码片段

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();
}
Run Code Online (Sandbox Code Playgroud)

但我没有获得cookie值.

这样的事情:c#获取httponly cookie

Baz*_*Baz 5

尝试从 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();
}
Run Code Online (Sandbox Code Playgroud)