2016-03-09 14 views
7

Tôi muốn đọc JSESSIONID từ cookie org.eclipse.swt.browser.Browser. Tôi cố gắng mở trình duyệt từ trình cắm thêm của Eclipse. Tôi đang sử dụng đoạn mã dưới đâyCách đọc cookie từ 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(); 
} 

Nhưng tôi không nhận được giá trị cookie.

Something như thế này: c# Get httponly cookie

+0

Tôi đã thử đoạn mã của bạn bằng 'http: // stackoverflow.com /'. Trước đó tôi đã sử dụng bộ kiểm tra Chromes để xem cookie nào thực sự được gửi (5 tất cả trong tất cả). Nhưng dường như SWT không thể nhìn thấy tất cả chúng. Không thể tìm thấy cái nào có tên là 'prov' và hết hạn vào năm 2055. Những cái có thể thấy hết hạn vào năm 2018 mới nhất. Có thể có một sự tương tự với JSESSIONID của bạn? –

Trả lời

2

Hãy thử nhận cookie từ JavaScript thay vì phương pháp Browser#getCookie(). Nó làm việc cho tôi trong thử nghiệm của tôi, nhưng như tôi không biết trang web của bạn, tôi không thể kiểm tra nó chống lại nó:

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

Tôi không nhận được JSESSIONID nhưng nhận các chi tiết khác từ cookie như cookie = true, v.v. – happy

+0

@happy Nếu bạn chỉ in các cookie trong vòng lặp, nó không chứa cookie? Bạn có thể xác minh trong trình duyệt thực tế mà cookie được đặt không? – Baz

+0

Trong trình duyệt thực tế, tôi có thể thấy JSESSIONID = ..., cookie = true, NSC_TMAA = .., NSC_TMAS = ..; Khi tôi gỡ lỗi mã tôi chỉ nhận được cookie = true, NSC_TMAA = .., NSC_TMAS = ..; Tôi đoán đó là vấn đề của httponly được đặt thành true. – happy

1

Nếu cookie bạn muốn nhận được đánh dấu là httpOnly sau đó bạn sẽ không có thể nhận được nó trong các phiên bản SWT hiện tại. Xem this bug để thảo luận.

Các vấn đề liên quan