{"record":{"id":"0d64c39aded181e3","repo":"didi/DoKit","slug":"cannot-set-request-property-after-connection-is-ma","errorCode":null,"errorMessage":"Cannot set request property after connection is made","messagePattern":"Cannot set request property after connection is made","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java","lineNumber":685,"sourceCode":"            if (proxy != null) return true;\n            Proxy clientProxy = client.proxy();\n            return clientProxy != null && clientProxy.type() != Proxy.Type.DIRECT;\n        }\n\n        @Override\n        public String getResponseMessage() throws IOException {\n            return getResponse(true).message();\n        }\n\n        @Override\n        public int getResponseCode() throws IOException {\n            return getResponse(true).code();\n        }\n\n        @Override\n        public void setRequestProperty(String field, String newValue) {\n            if (connected) {\n                throw new IllegalStateException(\"Cannot set request property after connection is made\");\n            }\n            if (field == null) {\n                throw new NullPointerException(\"field == null\");\n            }\n            if (newValue == null) {\n                return;\n            }\n\n            requestHeaders.set(field, newValue);\n        }\n\n        @Override\n        public void setIfModifiedSince(long newValue) {\n            super.setIfModifiedSince(newValue);\n            if (ifModifiedSince != 0) {\n                requestHeaders.set(\"If-Modified-Since\", format(new Date(ifModifiedSince)));\n            } else {\n                requestHeaders.removeAll(\"If-Modified-Since\");","sourceCodeStart":667,"sourceCodeEnd":703,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java#L667-L703","documentation":"setRequestProperty() is only legal before the connection is established; the wrapper's 'connected' flag flips to true inside buildCall() (triggered by connect, output/input streams, response code), after which setRequestProperty throws IllegalStateException(\"Cannot set request property after connection is made\"). This preserves the standard HttpURLConnection contract.","triggerScenarios":"Calling setRequestProperty() (or setRequestProperty-based helpers like setIfModifiedSince after connect) once connect()/getOutputStream()/getInputStream()/getResponseCode() has run.","commonSituations":"Setting an Authorization header lazily inside a retry loop; interleaving header setup with streaming-body writes; auth interceptors that patch headers late.","solutions":["Set all request properties before the first connect-triggering call.","In retry loops, create a new connection and re-apply all headers from a saved map.","Review code paths where interceptors/callbacks mutate the connection after writing starts."],"exampleFix":"// before\nconn.connect();\nconn.setRequestProperty(\"Authorization\", token); // ISE\n\n// after\nconn.setRequestProperty(\"Authorization\", token);\nconn.connect();","handlingStrategy":"validation","validationCode":"// apply every header from a map before any connect-triggering call:\nfor (Map.Entry<String,String> e : headers.entrySet()) conn.setRequestProperty(e.getKey(), e.getValue());","typeGuard":null,"tryCatchPattern":"try { conn.setRequestProperty(k, v); } catch (IllegalStateException ignored) { /* already connected: reopen connection instead */ }","preventionTips":["Enforce 'headers first, then connect, then body' ordering in all HTTP helper code.","For retries, rebuild a fresh connection and re-apply headers from your own map."],"tags":["network","http","urlconnection","state-machine"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}