Java cookie not adding to browser - java

CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
CookieStore cookieJar = manager.getCookieStore();
// create cookie
HttpCookie cookie = new HttpCookie("UserName", "John Doe");
// add cookie to CookieStore for a
// particular URL
URL url = new URL("http://localhost");
cookieJar.add(url.toURI(), cookie);
As I'v readed this code adds a cookie to the browser with every http request, but I've checked the browser cookies, and there's no UserName what is missing in this code?

As I'v readed this code adds a cookie to the browser
Not the browser but to the requests made by Java when your code use a url. When you'll do url.openConnection() the cookie will be added.

Related

How to get Cookies with HttpURLConnection in Java?

When I use HttpURLConnection and try con.getHeaderField("Set-Cookie") I get this response:
__cfduid=1111111aaaaaa; expires=Wed, 19-Dec-18 06:19:46 GMT; path=/; domain=.site.com; HttpOnly
But the browser cookies are:
__cfduid=1111111aaaaaa; _ym_uid=000000000; PHPSESSID=zzzzzzzz; _ym_isad=1; key=555
How I can get the FULL cookie, using HttpURLConnection? The most important cookie for me is key.
The value of Set-cookie header modify or append new value to Cookies in browser. And browser delete expired cookie from cookies. The assembling work completed by browser.
When request web in java, programmer need assemble 'full' cookies by Set-cookie header in single or multi responses.
If you use HttpURLConnection, you can use CookieManager
This is an example
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
URL url = new URL("https://stackoverflow.com");
URLConnection connection = url.openConnection();
connection.getContent();
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
for (HttpCookie cookie : cookies) {
System.out.println(cookie.getDomain());
System.out.println(cookie);
}
When you send HTTP request, CookieManager will auto fill Cookie Header. And, the value can be directly achieved from CookieManger by domain.

How to read persistent cookies stored by browser in java web start application

I have a java web start application.
Before launching the application web page stores a persistent cookie.
This is have the cookie has created: (.asp page):
Response.Cookies("MyApp")("Test") = "SomeValue"
Response.Cookies("Myapp").Expires = DateAdd("yyyy", 1, Now())
I can see the cookie inside chrome.
And this is how i tried to read the cookies:
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
CookieStore cookieJar = manager.getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie : cookies) {
System.out.println("CookieHandler retrieved cookie: "
+ cookie);
}
How can i read that cookie in my application?
Have you taken a look at this ? It has an example of Accessing Cookies from a java web start application.

setting Cookie: JSESSIONID on client request manually

im making a swing application which will sign in to a server; were im using HttpURLConnection to submit my request and get my response.
problem is when the httpRequest gets to the server the "Cookie: JSESSIONID" header is there, session id is there; but the request.getSession(false) will always return null.
here is the code which i use to set the header on the client:
connection.setRequestProperty("Cookie: JSESSIONID", client.getSessionId());
any help would be apprectiated
HttpPost httppost = new HttpPost(postData);
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId());
//cookie.setDomain("your domain");
cookie.setPath("/");
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
response = client.execute(httppost);
See also this Java: How to make a HTTP browsing session and this Apache HttpClient 4.0.3 - how do I set cookie with sessionID for POST request

Setting and Retrieving Cookies using HttpComponents

I am trying to figure out how to set and also retrieve cookies using HttpComponents, but I can't find solid documentation, especially when it comes to setting cookies on the request. What I have seems to work, but at the same time I can't confirm that the cookies I set are being sent correctly.
I notice that the cookie that I set on the request is also in the CookieStore after calling client.execute(), but I'm not sure if that's just because I add it to the CookieStore before calling client.execute() (maybe it stays in the CookieStore without being actually sent with the request?). Is there a good way to confirm the cookie is sent?
HttpGet get = new HttpGet("http://example.com/");
DefaultHttpClient client = new DefaultHttpClient();
// set the cookies
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("foo", "bar");
cookie.setDomain("example.com");
cookie.setPath("/something/");
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
// get the cookies
HttpResponse response = client.execute(get);
List<Cookie> cookies = client.getCookieStore().getCookies();
just found the follwoing example which demonstrates the use of cookies in an login example: HttpComponents Example with Cookies
Maybe you can modify this in a way what the server responds with the content of the cookie sent, so you can eval if the cookie really was sent to the server. (You send cookie with "foo", "bar" or some randomize values and the server will respond with "bar", "foo" or something like that)

Java get Session Cookies

Getting header fields from a URLConnection doesn't get session cookies for me.
When I use CookieManager I can get session cookies from a URL:
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
URLConnection con = url.openConnection();
con.getContent();
CookieStore cookieJar = manager.getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
This is fine, but I need to send a POST request. So I am writing to the URLConnection's output stream. My question is how to get the session cookies after sending the POST request.
Try using the same CookieManager object with your first and subsequent requests using URLConnection.

Categories

Resources