对于我的生命,我不明白为什么我不能访问文档的cookie的applet的起源。相同的页面,相同的IP地址。当我真正通过Java建立连接时,我看到正在发送的cookie(在wireshark中),所以我知道它们在那里。
有没有人有其他方法可以尝试从Java访问文档cookie ?我已经在互联网上搜索了,似乎找不到任何可能工作除了getRequestProperty()
方法!
我有以下applet代码:
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);
}
}
还有什么方法可以让JSObject
窗口到我的URLConnection
?否则,它会将我置于当前applet窗口的上下文中…
HttpURLConnection.getRequestProperty
只返回在连接通过connect建立时通过addresquestproperty发送的添加到其中的内容。响应报头可能包含Cookie或Set-Cookie报头,这些报头可以让您了解来自服务器的Cookie。
获取Applet页面的cookie:
1.像下面这样定义一个javascript函数——它应该是包含applet的页面的一部分。
function getDocumentCookies() { return document.cookie; }
设置要调用的jsobject,例如:
private String getDocumentCookies() { JSObject window = (JSObject)JSObject.getWindow(this); return window.call ("getDocumentCookies", new String[0]); }