Java get Session Cookies - java

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.

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 send cookies over URL connection

I am trying to set Cookies and request headers via URL connection. Here is the client side code
UUID = request.getHeader("UUID");
conn.addRequestProperty("Cookie", iPlanetDirectoryPro);
conn = url.openConnection();
conn.setDoOutput(true);
objOstr = new OutputStreamWriter(conn.getOutputStream());
objOstr.write(res);
One server side i am trying a retrieve the cookie using "iPlanetDirectoryPro" as the cookie name. But i am getting null. What is the mistake i am doing here?
If i set as conn.addRequestProperty("iPlanetDirectoryPro", iPlanetDirectoryPro); then what is the difference between cookie-and-string-in-request-header https://stackoverflow.com/questions/21226475/difference-between-cookie-and-string-in-request-header
Try using:
conn.getRequestProperty("Cookies");
See the doc.

Java cookie not adding to browser

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.

Android setRequestProperty in a url.openConnection()

I have an Android app that need to set a requestproperty in a connection. Here is my code:
URL url = new URL(sUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("cookie", cookievalue);
connection.connect();
When I call the setRequestProperty method it launch the exception:
java.lang.IllegalStateException: Cannot set request property after connection is made
Is there a way to create the connection to the file without using the url.openConnection() ?
Here url.openCOnnection() will open new connection to the resource referred to by this URL.
Here you again opening a connection by calling url.connect() method. So remove that
Check this.. for the sample example...
You could try to use the CookieManager mentioned in http://developer.android.com/reference/java/net/HttpURLConnection.html
Set your cookie to CookieManager
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
HttpCookie cookie = new HttpCookie("lang", "fr");
cookie.setDomain("twitter.com");
cookie.setPath("/");
cookie.setVersion(0);
cookieManager.getCookieStore().add(new URI("http://twitter.com/"), cookie);
Source: http://developer.android.com/reference/java/net/HttpURLConnection.html
Use url.openConnection() after you set your cookie.

How to handle multiple requests using HttpURLConnection in a MVC app?

I have a MVC application and I'd like that each request must handle its own session.
My request:
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
As we know CookieHandler allow just one session per application. I'd like that each request creates and use its own session.
Does anybody know how to do it?
Thanks
you will have to use Proxy while opening a connection...
the use of proxy provides always a new IP address for the server so you can be sure that the server maintains different session for each request...
your code will be something like following...
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy("some_proxy"));

Categories

Resources