didi/DoKit · error · IllegalStateException

Cannot add request property after connection is made

Error message

Cannot add request property after connection is made

What it means

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.

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java:710

            }

            requestHeaders.set(field, newValue);
        }

        @Override
        public void setIfModifiedSince(long newValue) {
            super.setIfModifiedSince(newValue);
            if (ifModifiedSince != 0) {
                requestHeaders.set("If-Modified-Since", format(new Date(ifModifiedSince)));
            } else {
                requestHeaders.removeAll("If-Modified-Since");
            }
        }

        @Override
        public void addRequestProperty(String field, String value) {
            if (connected) {
                throw new IllegalStateException("Cannot add request property after connection is made");
            }
            if (field == null) {
                throw new NullPointerException("field == null");
            }
            if (value == null) {
                return;
            }

            requestHeaders.add(field, value);
        }

        @Override
        public void setRequestMethod(String method) throws ProtocolException {
            if (!METHODS.contains(method)) {
                throw new ProtocolException("Expected one of " + METHODS + " but was " + method);
            }
            this.method = method;
        }

View on GitHub (pinned to 626827cddb)

Solutions

  1. Collect all headers (including repeated ones) and apply them before any connect-triggering call.
  2. Reorder code so addRequestProperty runs before getOutputStream().
  3. Use a new connection instance for retries and re-add all headers.

Example fix

// before
conn.connect();
conn.addRequestProperty("Cookie", cookie); // ISE

// after
conn.addRequestProperty("Cookie", cookie);
conn.connect();
Defensive patterns

Strategy: validation

Validate before calling

// add all multi-value headers before connect():
for (String v : values) conn.addRequestProperty(name, v);

Prevention

When it happens

Trigger: Calling addRequestProperty() after connect()/getOutputStream()/getInputStream()/getResponseCode(); adding Cookie or Set-of-values headers late in the request lifecycle.

Common situations: 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.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/12c5f5c9598752ce. Report an issue: GitHub.