Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setRequestProperty throwing java.lang.IllegalStateException: Cannot set request property after connection is made

Tags:

android

I am getting java.lang.IllegalStateException:

java.lang.IllegalStateException: Cannot set request property after connection is made error when setRequestProperty method is called after url.openConnection();

Here is what i am trying:

URL url = new URL("https://49.205.102.182:7070/obsplatform/api/v1/mediadevices/545b801ce37e69cc");
         urlConnection = (HttpsURLConnection) url
        .openConnection();
urlConnection.setRequestProperty("Content-Type","application/json");

any suggestions please? Thanks in advance.

like image 875
siva Avatar asked Mar 11 '14 08:03

siva


4 Answers

This usually happens if you have in the debug watchers calls, such as conn.getResponseCode() or anything that queries the request result before the request was actually issued or completed. This causes, that during debug, a request is performed by the watcher, before having properly set you request, and then it becomes invalid.

like image 124
Pablo Retyk Avatar answered Oct 31 '22 22:10

Pablo Retyk


I only have this issue while in debugging mode, Run without debugging (You can print logs) everything should work fine

like image 24
kyildizoglu Avatar answered Oct 31 '22 21:10

kyildizoglu


The obvious thing is to think that you need to add properties before calling open on the URL. this however is not the case. i have seen many samples of settings being set AFTER url has been open (as counter intuitive as that is).

the problem in my case is that i had conn.getResponseCode() added in my watch list. removed that and all good.

... tricky.

like image 2
Squibly Avatar answered Oct 31 '22 22:10

Squibly


please check below code

HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();
like image 1
Kanaiya Katarmal Avatar answered Oct 31 '22 22:10

Kanaiya Katarmal