David
David

Reputation: 150

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

Upvotes: 2

Views: 4663

Answers (2)

Alex Ander
Alex Ander

Reputation: 36

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.

Upvotes: 1

Jagadesh Seeram
Jagadesh Seeram

Reputation: 2664

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

Upvotes: 2

Related Questions