[英]Java cookies using URLConnection
我有一个为localhost:9090 / application创建cookie的方法。
public Object makeCookie(String p) throws IOException{
URL myUrl = new URL("localhost:9090/application");
URLConnection urlConn = myUrl.openConnection();
urlConn.setRequestProperty("testCookie", p);
urlConn.connect();
return urlConn;
}
我有一个方法应该打印刚刚设置的cookie的名称和域,但是没有任何结果。
CookieManager cookieManager;
URL url;
URLConnection connection;
CookieStore cookieStore;
List<HttpCookie> cookieList;
public boolean checkone (String test1) throws ClassNotFoundException, IOException{
cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
url = new URL("localhost:9090/application/");
connection = url.openConnection();
connection.getContent();
cookieStore = cookieManager.getCookieStore();
cookieList = cookieStore.getCookies();
for (HttpCookie cookie: cookieList){
System.out.println("Domain: " + cookie.getDomain());
System.out.println("name of cookie: " + cookie.getName());
}
return true;
}
创建cookie时我是否缺少某些东西?
我认为您没有以正确的方式获取Cookie。 cookie在请求的标头中设置。 我在您的示例中看不到。 看看这个:
https://examples.javacodegeeks.com/core-java/net/urlconnection/get-cookies-from-http-connection/
还要看看这个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.