[英]Java cookies using URLConnection
I have a method that creates a cookie for localhost:9090/application. 我有一个为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;
}
I have a method that should print the name and domain of the cookie I just set, but I am getting no results. 我有一个方法应该打印刚刚设置的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;
}
Am I missing something when creating the cookie? 创建cookie时我是否缺少某些东西?
I do not think you are getting the cookies the right way. 我认为您没有以正确的方式获取Cookie。 The cookies are set in the headers of the request. cookie在请求的标头中设置。 I do not see that in your example. 我在您的示例中看不到。 Take a look at this: 看看这个:
https://examples.javacodegeeks.com/core-java/net/urlconnection/get-cookies-from-http-connection/ https://examples.javacodegeeks.com/core-java/net/urlconnection/get-cookies-from-http-connection/
Take a look at this also: 还要看看这个:
How to set Cookies at Http Get method using Java 如何使用Java在Http Get方法中设置Cookies
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.