2016-03-09 21 views
7

JSESSIONID numaralı kurabiyeleri org.eclipse.swt.browser.Browser numaralı çerezden okumak istiyorum. Eclipse eklentisinden tarayıcıyı açmaya çalışıyorum. aşağıda kullanarakÇerez nasıl org.eclipse.swt.browser.Browser?

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

pasajı Ama çerez değerini almıyorum ediyorum. Böyle

şey: c# Get httponly cookie

+0

Snippet'inizi http: // stackoverflow.com/'ile denedim. Bundan önce, hangi tanımlama bilgilerinin gönderildiğini görmek için Chromes denetçisini kullandım (hepsinde 5 tane). Ama görünüşe göre SWT hepsini göremez. Bulunamayan, "prov" olarak isimlendirilmiş ve 2055 yılında sona ermektedir. Görülebilenler en son 2018'de sona ermektedir. Belki de JSESSIONID'inizle bir benzerlik var mı? –

cevap

2

Browser#getCookie() yöntemin yerine JavaScript çerezi edinmeyi deneyin. Bu benim test sırasında benim için çalıştı, ama ben sitenizi bilmiyorum, ben buna karşı test edemez: istediğiniz çerez httpOnly olarak işaretlenmiş almak durumunda

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

JSESSIONID alamıyorum, ancak cookie = true vb. – happy

+0

@happy Çerezleri döngüde yazdırıyorsanız, bu çerez içermiyor mu? Çerezin ayarlandığı gerçek bir tarayıcıda doğrulayabilir misiniz? – Baz

+0

Gerçek tarayıcıda JSESSIONID = ..., cookie = true, NSC_TMAA = .., NSC_TMAS = ..; Kodda hata ayıkladığımda sadece cookie = true, NSC_TMAA = .., NSC_TMAS = ..; .Sanırım httponly'nin doğru olarak ayarlanmış problemi. – happy

1

o zaman olmaz mevcut SWT sürümlerinde elde edebilmek. Bir tartışma için bkz. this bug.