简体   繁体   English

Java Applet获取文档Cookie?

[英]Java applet get document cookies?

And for the life of me I can't figure out why I can't access the document's cookies for the applet's origin. 对于我的一生,我无法弄清楚为什么我无法访问该applet来源的文档cookie。 Same page, same IP address. 相同页面,相同IP地址。 When I actually make the connection via Java, I see the cookies being sent (in wireshark), so I know they're there. 当我实际上通过Java建立连接时,我看到了cookie(在wireshark中)发送了,所以我知道它们在那里。

Does anyone have any alternate ways to try to access the document cookies from Java? 是否有人可以尝试从Java访问文档cookie的其他方法? I've scoured around the internet, and can't seem to find anything that might work besides the getRequestProperty() method! 我到处搜寻了互联网,除了getRequestProperty()方法外,似乎找不到任何可行的方法!

I have the following applet code: 我有以下小程序代码:

import java.applet.*;
import java.net.*;
import java.util.*;
import java.io.*;
import netscape.javascript.*;

public class test extends Applet {
    public void init() {
    try {
        URL url = new URL("http://10.0.0.5/java/test.html");            
        String inputLine;
        URLConnection conn = url.openConnection();

        System.out.print("Cookies:\n");
        String m = conn.getRequestProperty("Cookie");
        // Returns null :-/
        System.out.println(m);

        // Read page content => works fine... (sends cookie)
        //BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        //while ((inputLine = in.readLine()) != null) 
        //    System.out.println(inputLine);
        //in.close();        
    } catch (Exception e) { 
        System.out.println("Error :(");
        System.out.println(e.getMessage());
    }
    String alert = "alert(document.cookie);";
    JSObject win = (JSObject) JSObject.getWindow(this);
    win.eval(alert);
    }
}

Also is there any way to get the JSObject window to my URLConnection ? 也有什么方法可以将JSObject窗口连接到我的URLConnection吗? Otherwise it sets me in the context of the current applet window... 否则,它将使我处于当前小程序窗口的上下文中。

HttpURLConnection.getRequestProperty

only returns what you add to it via the addRequestProperty to be sent when the connection is made via connect. 仅在通过connect建立连接时通过addRequestProperty返回添加到其中的内容。 The response headers may contain Cookie or Set-Cookie headers which can give you some clue about the cookies from the server. 响应标头可能包含Cookie或Set-Cookie标头,可以为您提供有关服务器中Cookie的一些线索。

To get cookies for your Applet's page: 要获取Applet页面的Cookie,请执行以下操作:

  1. 1.define a javascript function such as the following - this should be part of the page containing your applet. 1.定义以下javascript函数-这应该是包含小程序的页面的一部分。

     function getDocumentCookies() { return document.cookie; } 
  2. se jsobject to invoke, for example: 调用的jsobject,例如:

     private String getDocumentCookies() { JSObject window = (JSObject)JSObject.getWindow(this); return window.call ("getDocumentCookies", new String[0]); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM