Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top