2

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

user987339
  • 10,519
  • 8
  • 40
  • 45
David
  • 150
  • 1
  • 10

2 Answers2

2

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

Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
1

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.

Alex Ander
  • 36
  • 4