{"record":{"id":"6593c5bc7350a0ac","repo":"didi/DoKit","slug":"cannot-access-request-header-fields-after-connecti","errorCode":null,"errorMessage":"Cannot access request header fields after connection is set","messagePattern":"Cannot access request header fields after connection is set","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java","lineNumber":445,"sourceCode":"                return headers.name(position);\n            } catch (IOException e) {\n                return null;\n            }\n        }\n\n        @Override\n        public Map<String, List<String>> getHeaderFields() {\n            try {\n                return toMultimap(getHeaders(), statusLineToString(getResponse(true)));\n            } catch (IOException e) {\n                return Collections.emptyMap();\n            }\n        }\n\n        @Override\n        public Map<String, List<String>> getRequestProperties() {\n            if (connected) {\n                throw new IllegalStateException(\n                        \"Cannot access request header fields after connection is set\");\n            }\n\n            return toMultimap(requestHeaders.build(), null);\n        }\n\n        @Override\n        public InputStream getInputStream() throws IOException {\n            if (!doInput) {\n                throw new ProtocolException(\"This protocol does not support input\");\n            }\n\n            Response response = getResponse(false);\n            if (response.code() >= HTTP_BAD_REQUEST) {\n                Log.e(\"DoKit\", \"FileNotFoundException:\" + url.toString());\n                return ConvertUtils.string2InputStream(\"FileNotFoundException:\" + url.toString(), \"utf-8\");\n            }\n","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java#L427-L463","documentation":"In OkHttpURLConnection, getRequestProperties() must return the request headers before the connection is established; the wrapper tracks a 'connected' flag (set true in buildCall()) and throws IllegalStateException if headers are read after that point. This matches HttpURLConnection's contract that request properties are inaccessible once connected.","triggerScenarios":"Calling getRequestProperties() after connect(), getOutputStream(), getInputStream(), getResponseCode(), or any other call that triggers buildCall()/getResponse().","commonSituations":"Logging or debugging code that dumps request headers after firing the request; retry wrappers that read request properties to rebuild a request after the first attempt already connected.","solutions":["Snapshot getRequestProperties() (or your own header map) before the first connect-triggering call.","Move header logging to before getOutputStream()/getInputStream().","For retries, keep the original headers in your own map instead of re-reading them from the connection."],"exampleFix":"// before\nconn.getOutputStream().write(body);\nMap<String, List<String>> props = conn.getRequestProperties(); // ISE\n\n// after\nMap<String, List<String>> props = conn.getRequestProperties();\nconn.getOutputStream().write(body);","handlingStrategy":"validation","validationCode":"Map<String, List<String>> snapshot = new LinkedHashMap<>();\n// fill snapshot as you set headers; read it after connect instead of conn.getRequestProperties()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Maintain your own header map alongside the connection; never query the connection for request state after connect.","Treat connect()/getOutputStream()/getInputStream() as point-of-no-return calls."],"tags":["network","http","state-machine","urlconnection"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}