SetRequestProperty throwing java.lang.IllegalStateException: unable to set request property after connection

I get java.lang.IllegalStateException :

java.lang.IllegalStateException : Unable to set the request property after the connection made an error when the setRequestProperty method setRequestProperty 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.

+6
source share
6 answers

This can happen if you have some calls in the debugging observer, such as conn.getResponseCode () (or anything that executes the request, while it has not yet completed). This leads to the fact that during debugging, the request is executed by the debugger observer before the request is correctly configured and then it becomes invalid.

+21
source

I have this problem only in debug mode, Starting without debugging (you can print logs) everything should work fine

+4
source

Obviously, you need to add properties before opening the URL. this, however, is not so. I saw a lot of sample settings set by AFTER url was open (as counter intuitively like this).

The problem in my case is that conn.getResponseCode () was added to my watchlist. deleted it and all good.

... complicated.

+1
source

I got the same exception on setRequestProperty("Range","byte=" + downloadedSize + "-") . After adding connection.setChunkedStreamingMode(0); the problem is gone

0
source

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(); 
-1
source

I have the same problem. I watched this problem on Nexus 5. My application code constantly fails with the same exception (or his twin brother โ€œcannot set the request methodโ€.)

What I noticed is that this happens if I leave the phone for a while. One of them starts to fail, it fails all the time - but if I restart the phone / emulator again, then everything is fine).

My suspicion is either a pooling error on the side of the framework, or a leak somewhere in the code resources.

-1
source

All Articles