{"record":{"id":"12c5f5c9598752ce","repo":"didi/DoKit","slug":"cannot-add-request-property-after-connection-is-ma","errorCode":null,"errorMessage":"Cannot add request property after connection is made","messagePattern":"Cannot add 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":710,"sourceCode":"            }\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\");\n            }\n        }\n\n        @Override\n        public void addRequestProperty(String field, String value) {\n            if (connected) {\n                throw new IllegalStateException(\"Cannot add request property after connection is made\");\n            }\n            if (field == null) {\n                throw new NullPointerException(\"field == null\");\n            }\n            if (value == null) {\n                return;\n            }\n\n            requestHeaders.add(field, value);\n        }\n\n        @Override\n        public void setRequestMethod(String method) throws ProtocolException {\n            if (!METHODS.contains(method)) {\n                throw new ProtocolException(\"Expected one of \" + METHODS + \" but was \" + method);\n            }\n            this.method = method;\n        }","sourceCodeStart":692,"sourceCodeEnd":728,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java#L692-L728","documentation":"addRequestProperty() appends a header (allowing repeated fields) but is likewise only legal before connection; once the wrapper's connected flag is set by buildCall(), it throws IllegalStateException(\"Cannot add request property after connection is made\"). Identical state machine to setRequestProperty, but for multi-valued headers.","triggerScenarios":"Calling addRequestProperty() after connect()/getOutputStream()/getInputStream()/getResponseCode(); adding Cookie or Set-of-values headers late in the request lifecycle.","commonSituations":"Cookie managers adding cookies right before writing the body but after connect; multi-value header setup split across helper methods where one helper already triggered the connection.","solutions":["Collect all headers (including repeated ones) and apply them before any connect-triggering call.","Reorder code so addRequestProperty runs before getOutputStream().","Use a new connection instance for retries and re-add all headers."],"exampleFix":"// before\nconn.connect();\nconn.addRequestProperty(\"Cookie\", cookie); // ISE\n\n// after\nconn.addRequestProperty(\"Cookie\", cookie);\nconn.connect();","handlingStrategy":"validation","validationCode":"// add all multi-value headers before connect():\nfor (String v : values) conn.addRequestProperty(name, v);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Batch repeated headers (cookies, accepts) into the pre-connect setup phase.","Never mutate a connection that a helper may already have connected."],"tags":["network","http","urlconnection","state-machine","headers"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}