Android setRequestProperty in a url.openConnection() - java

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.

Related

How to set cookie from CookieManager when using HttpUrlConnection?

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().

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.

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"));

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.

open url connection and wait to finish loading of the url java

i have the following code. i want to get the url after finished loading the url. How can i do my connection to wait to finish the loading first?
Thank you in advance
URL u = new URL("http://google.com/search?sourceid=navclient&btnI=1&q=" + jTextField1.getText().toString());
HttpURLConnection con = (HttpURLConnection) u.openConnection();
HttpURLConnection ex1 = (HttpURLConnection) u.openConnection();
ex1.setChunkedStreamingMode(7000); // enable chunked streaming mode before connecting to server.

Categories

Resources