How to read cookie from org.eclipse.swt.browser.Browser?
Asked Answered
M

2

9

I want to read JSESSIONID from cookie org.eclipse.swt.browser.Browser. I try to open browser from Eclipse plug-in. I am using below snippet

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();
}

But I am not getting cookie value.

Something like this : c# Get httponly cookie

Murillo answered 9/3, 2016 at 12:35 Comment(1)
I tried your snippet with http://stackoverflow.com/. Before that I used Chromes inspector to see what cookies are actually sent (5 all in all). But apparently SWT cannot see all of them. The one that could not be found is named prov and expires in 2055. The ones that can be seen expire in 2018 latest. Maybe there is an analogy to your JSESSIONID?Huckster
M
5

Try getting the cookie from JavaScript instead of the Browser#getCookie() method. It worked for me during my test, but as I don't know your website, I can't test it against it:

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();
}
Malayoindonesian answered 13/3, 2016 at 12:53 Comment(6)
I am not getting JSESSIONID but getting other details from cookie like cookie=true,etc.Murillo
@Murillo If you just print the cookies in the loop, it doesn't contain the cookie? Can you verify in an actual browser that the cookie is set?Malayoindonesian
In actual browser i can see JSESSIONID=...,cookie=true,NSC_TMAA=..,NSC_TMAS=..; When I debug the code i get only cookie=true,NSC_TMAA=..,NSC_TMAS=..; .I guess it is the problem of httponly set to true.Murillo
@Murillo Quite possible, yes.Malayoindonesian
@Murillo Can you set httponly to false as a test?Malayoindonesian
yes i am able to get JSESSIONID on httponly with false :(..Now I am trying to read it through XMLHttpRequest.Murillo
P
1

If the cookie you wish to get is marked as httpOnly then you won't be able to get it in current SWT versions. See this bug for a discussion.

Promising answered 18/3, 2016 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.