如何从 org.eclipse.swt.browser.Browser 中读取 cookie?
How to read cookie from org.eclipse.swt.browser.Browser?
我想从 cookie org.eclipse.swt.browser.Browser
中读取 JSESSIONID
。我尝试从 Eclipse 插件打开浏览器。
我正在使用下面的代码片段
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Whosebug");
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 值。
像这样:
尝试从 JavaScript 而不是 Browser#getCookie()
方法获取 cookie。它在我的测试期间对我有用,但由于我不知道你的网站,我无法对其进行测试:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Whosebug");
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();
}
如果您希望获取的 cookie 被标记为 httpOnly
那么您将无法在当前的 SWT 版本中获取它。请参阅 this bug 进行讨论。
我想从 cookie org.eclipse.swt.browser.Browser
中读取 JSESSIONID
。我尝试从 Eclipse 插件打开浏览器。
我正在使用下面的代码片段
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Whosebug");
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 值。
像这样:
尝试从 JavaScript 而不是 Browser#getCookie()
方法获取 cookie。它在我的测试期间对我有用,但由于我不知道你的网站,我无法对其进行测试:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Whosebug");
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();
}
如果您希望获取的 cookie 被标记为 httpOnly
那么您将无法在当前的 SWT 版本中获取它。请参阅 this bug 进行讨论。