Get cookies set for subdomain - java

so I am using Jsoup and I am trying to get the cookies set for the site subdomain. The Connection.Response#cookies only returns the one set for that domain. I would like to grab the cookies so that I can use them to request data from the subdomain.
Example:
Connection.Response res = Jsoup.connect("https://www.tvplayer.com/watch/dave").userAgent("Mozilla/5.0").timeout(10000).method(Connection.Method.GET).execute();
Document doc = Jsoup.connect("https://live.tvplayer.com/").timeout(10000).cookies(res.cookies()).get();
If you request it throws a 403 due to the cookie not being set, this is because the cookies for the response doesn't have the ones from the subdomain live. just the main domain.
Outputting the cookies gives the cookies AWSELB and PHPSESSID only.

Related

Fetching cookie

I have a site www.mysite.com. This site is called from another site www.aaa.com where I stamp the cookie (say x = 1) from the server.
Now when my site is called from new site www.new.com, will I be able to fetch the cookie (x=1) that I had stamped from my server when my server was called from www.aaa.com ?
NOTE: All the above processes take place in the same browser.
I am trying to do access the cookie but not been able to do so. I am using vertx as the server.
Small snippet to fetch the cookies in the server:
public Set getAllCookiesName(RoutingContext context) {
Set<Cookie> cookies = context.cookies();
Set<String> cookieNames = new HashSet<String>();
for (Cookie cookie: cookies) {
cookieNames.add(cookie.getName());
}
return cookieNames;
}
Cookies are not shared across different domains, however they can be shared between domains and their subdomains. For e.g.if your mysite is subdomain of another site www.aaa.com (mysite.aaa.com) then cookies could be shared. However it would depend on what path you set your cookie into.

Save Cookie (Including !httponly flag) And Session From HttpURLConnection

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?

How to get HttpOnly cookie

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.

HttpServletRequest returning no cookies - but they exist

We have a portlet based application that retrieves a certain cookie for validation and then sends off an action request afterwards.
However, we're seeing an issue where the HttpServletRequest is returning a null list of cookies - even though I can 100% confirm there are cookies. In fact, sometimes the code DOES work and shows the cookies (although this is pretty rare).
We've noticed that IE appears to work more frequently than FF and Chrome, but again, there's no consistency or pattern really to determine what causes it to function.
The Request is always obtained - there's never an issue here of a null pointer. The only issue at this moment is that the cookie list is empty.
Method in static class that returns HttpServletRequest
FacesContext context = FacesContext.getCurrentInstance();
Map<String, Object> requests = context.getExternalContext().getRequestMap();
for (String requestName : requests.keySet()) {
if (requests.get(requestName) instanceof HttpServletRequest) {
return ((HttpServletRequest) requests.get(requestName));
}
}
return null;
Call from the other class to the static method above:
Cookie[] cookies = StaticClass.getHttpRequest().getCookies();
System.out.println("Cookies = " + cookies);
The HttpServletRequest method getCookies() should return an array of cookies the client sent in on the request. If the array returned is null, it means that the request the client sent in contains no cookies. Did you call addCookie(yourCookie) method within the same domain or subdomain (I ask as you cannot access cookies across different domains)?
ie:
Cookie yourCookie = new Cookie(name, value);
response.addCookie(yourCookie);
If the cookie was not added to a previous response, it will not be on the request.
Some other clues:
Check your Cookie's max age. Cookie may be expired.
Check the domain of the cookie and the domain of the request URL. If you are not sure, post them here for help.
In general if you can capture the http request message and post it here it will also be helpful.
Update: in firefox you can right click a page, and select 'view page info', then select the 'security' tab, click the 'view cookies' button to view all the cookies. You can also change the domain name in the popup window to see the cookies under other domain.

Java Servlet API 2.5 Cookie.getDomain() always returns null

I'm having an issue using the Cookie class of the Servlet API 2.5 on Tomcat . I pull out the list of cookies from the HttpServletRequest object and iterate over them like so:
Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies) {
System.out.println("Name=" + cookie.getName() + " Domain=" + cookie.getDomain());
}
However, for every single cookie in the request the Domain is null. Why is this? The reason I'm asking is because I have a cookie with the same name in two different domains and I want to be able to differentiate between them based on the domain. To help clarify the situation, my identically named cookies are being set in .anydomain.net and .subdomain.anydomain.net. Both are getting sent in the request but the domains are null when they get to the servlet. Is it expected behavior that the servlet cannot see the domain of cookies sent to it?
Edit: I set the cookies along with domain,expiration,and path in a previous request to the servlet. The next request coming into the browser with these cookies shows the domain as null. I have verified the cookies are getting set in the right domains in the browser.
Edit 2: I'm using Tomcat 6
Are you sure that you can get anything except the value from request cookies?
The browser will send only name=value in the HTTP Cookie header.
Other attributes (secure, domain, path, expiration) are only available for cookies that you set into the response yourself.
They are used to create the Set-Cookie response headers.
Properties such as domain are only used for a cookie when it is a part of the response (i.e. in Set-Cookie header). A client (such as a web browser) should only send the cookies that have the correct domain (path, etc.). The request thus only sees values because the header itself (Cookie) only contains values. Your client should not be sending cookies from different domains to the server.

Categories

Resources