Search code examples
javabrowserswtswt-awt

Open URL in new browser with new session and cookies using SWT


I want to open an URL in the external browser with the new session and cookies for the URL. every time the URL is opened it shall be opened in a new browser with new cookies. My problem is, whenever I try to open the URL in new browser the old session expires and the old browser window is no more useful. URL parameters will be different every time. I've tried to use the internal browser and well as external browser of SWT. tried Browser.setCookies(value,url); still doesn't work.

For External browser:

public void openUrl(String url){
    IWebBrowser browser = PlatformUI.getWorkbench()
        .getBrowserSupport()
        .createBrowser(IWorkbenchBrowserSupport.AS_EXTERNAL, browserId, name, tooltip); 
    browser.openUrl(url);
} 

For Internal browser:

public void launchUrl(String url){ 
    Browser browser = (Browser) getBrowserShellNew().getData(BROWSER); // Browser is a static String "BROWSER" 
    browser.setUrl(url); 
}

private Shell getBrowserShellNew(){ 
    Display display=PlatformUI.getWorkbench().getDisplay(); 
    Shell shell = new Shell(display); 
    shell.setText("title"); 
    Rectangle screenBounds = null; 
    if (display.getMonitors().length > 1) { 
        screenBounds = display.getMonitors()[1].getBounds();
    } else { 
        screenBounds = display.getMonitors()[0].getBounds(); 
    } 
    shell.setBounds(screenBounds); 
    shell.setLayout(new FillLayout()); 
    shell.setMaximized(true); 
    Browser browser = new Browser(shell, SWT.NONE); 
    browser.setMenu(new Menu(browser)); 
    shell.setData(BROWSER, browser); 
    shell.open(); 
    shell.forceActive(); 
    shell.forceFocus(); 
    return shell;
}

Reason must be, all browser instance uses the same cookies from the session. Need to somehow create a new session with cookies for every URL launch. Using external browser.


Solution

  • Well after some investigation I found the method to achieve this by passing the parameter to IE while launching the URL. as I've already mentioned in the comments to the answer. Below is the solution that worked for me.

    Runtime.getRuntime().exec("C:\\Program Files\\internet explorer\\iexplore.exe -noframemerging \"http://helloworld.com\"");
    

    Note: this will only work for IE and windows operating system.