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.
Related
I have a problem when used HttpURLConnection in Java, The connection to rest server give client cookie after authenticated. I can save the cookies from response header (Set-Cookie) after requesting and use them for next request but cookies that has !httponly flag is not available in response header (Set-Cookie).
I need the session_id cookie with !httponly flaged to be sent to next request so the connection no need to be authenticated in next request after the app exiting.
How can I save all cookies (basic cookies, secure cookies, httponly cookies) in Java just like cURL does?
After searching for a long time, I found the solution: http://jaunt-api.com/
I realize this question is old, but it was the first answer that came up in an internet search.
To resolve, use CookieHandler.setDefault( new CookieManager( null, CookiePolicy.ACCEPT_ALL ) ); per here: Should HttpURLConnection with CookieManager automatically handle session cookies?
I'm perform a http request with some cookie, My code like this:
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
cookieManager.getCookieStore().add(....);//here to set cookie
CookieHandler.setDefault(cookieManager);
// do request in following
But I can't find my cookie by fiddler. I have search many questions in stackoverflow, none of them work through. So, please help me to find the way to set cookie.
P.S. I don't want to use Cookie header.
finally , I found the problem , CookieHandler.setDefault() should be call before any Http operation, in my code ,it should before u.openConnection().
I need to get the HttpOnly cookies set as Session cookie in my java code.
To get non HttpOnly cookies I used Jsoup but now am stucked with HttpOnly cookies ?
Note :
Don't know if it matters but the site from which I want to get HttpCookies is developed in ASP.net.
I would expect Jsoup to make those available:
Connection connection = Jsoup.connect("http://example.com");
Connection.Response response = connection.execute();
...then use the cookies map on response.
If the HttpOnly cookies aren't there, you may have to read the contents of the URL directly (e.g., via URLConnection or similar) before passing them on to JSoup.parse, and look for the cookie headers in that stream.
Side note: To get a Document after connecting as with the above, without repeating the request, use Connection#parse:
Document doc = connection.parse();
I just mention this because you're likely to want the document as well as the cookies.
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.
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.