{"record":{"id":"aa83b281f789fd34","repo":"lingochamp/FileDownloader","slug":"connection-failed-with-request-s-response-s-ht","errorCode":null,"errorMessage":"Connection failed with request[%s] response[%s] http-state[%d] on task[%d-%d], which is changed after verify connection, so please try again.","messagePattern":"Connection failed with request\\[(.+?)\\] response\\[(.+?)\\] http-state\\[(.+?)\\] on task\\[(.+?)-(.+?)\\], which is changed after verify connection, so please try again\\.","errorType":"exception","errorClass":"SocketException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/liulishuo/filedownloader/download/DownloadRunnable.java","lineNumber":96,"sourceCode":"        do {\n\n            try {\n                if (paused) {\n                    return;\n                }\n\n                isConnected = false;\n                connection = connectTask.connect();\n                final int code = connection.getResponseCode();\n\n                if (FileDownloadLog.NEED_LOG) {\n                    FileDownloadLog\n                            .d(this, \"the connection[%d] for %d, is connected %s with code[%d]\",\n                                    connectionIndex, downloadId, connectTask.getProfile(), code);\n                }\n\n                if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) {\n                    throw new SocketException(FileDownloadUtils.\n                            formatString(\n                                    \"Connection failed with request[%s] response[%s] \"\n                                            + \"http-state[%d] on task[%d-%d], which is changed\"\n                                            + \" after verify connection, so please try again.\",\n                                    connectTask.getRequestHeader(),\n                                    connection.getResponseHeaderFields(),\n                                    code, downloadId, connectionIndex));\n                }\n\n                isConnected = true;\n                final FetchDataTask.Builder builder = new FetchDataTask.Builder();\n\n                if (paused) return;\n                fetchDataTask = builder\n                        .setDownloadId(downloadId)\n                        .setConnectionIndex(connectionIndex)\n                        .setCallback(callback)\n                        .setHost(this)","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/lingochamp/FileDownloader/blob/6237a8cac174bcc916e4342b14ab1ab72a5768d4/library/src/main/java/com/liulishuo/filedownloader/download/DownloadRunnable.java#L78-L114","documentation":"A SocketException thrown in DownloadRunnable.run when a per-connection response code is neither HTTP 206 Partial Content nor HTTP 200 OK during a multi-connection download. The message indicates the server's response changed after the trial/verify connection succeeded — i.e., the connection that previously validated now returns an unexpected status. The task should simply be retried.","triggerScenarios":"During multi-connection downloads, any connection receiving a code other than 200/206 after the initial verify connection returned a valid code — e.g. server began returning 403/404/5xx, rate-limited, or dropped Range support between the verify request and the actual range requests.","commonSituations":"Flaky CDNs or load balancers with inconsistent behavior across nodes; signed URLs expiring between verify and download; servers that intermittently reject Range requests; unstable networks causing server-side session loss; heavy server load returning 5xx.","solutions":["Simply retry the download — the message explicitly says 'please try again' (FileDownloader handles retries; ensure retryCount is set appropriately)","Catch SocketException in the FileDownloadListener error callback and restart the task","Verify the URL/token lifetime is long enough that signed URLs do not expire mid-download","Test server consistency: confirm the URL reliably returns 200/206 and supports Range (curl -H 'Range: bytes=0-99' -I)","Pin to a different/more stable CDN endpoint or mirror URL"],"exampleFix":"// before: single-attempt listener\nFileDownloader.getImpl().create(url).setPath(path).start(listener);\n// after: let the library retry and handle residual errors\nFileDownloader.getImpl().create(url)\n    .setPath(path)\n    .setRetryCount(3)\n    .start(new FileDownloadListener() {\n        @Override public void error(FileDownloadTask task, Throwable e) {\n            if (e instanceof SocketException) {\n                // retry manually with backoff or switch to fallback URL\n            }\n        }\n    });","handlingStrategy":"retry","validationCode":"HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();\nc.setRequest(\"Range\", \"bytes=0-99\");\nint code = c.getResponseCode();\nif (code != 206 && code != 200) throw new IllegalStateException(\"Server unstable for range downloads, code=\" + code);","typeGuard":null,"tryCatchPattern":"try {\n    FileDownloader.getImpl().create(url).setPath(path).setRetryCount(3).start(listener);\n} catch (SocketException e) {\n    // server changed response between verify and download; retry with backoff or switch URL\n    retryWithBackoffOrFallbackUrl();\n}","preventionTips":["Set a retry count on tasks so transient server flips are absorbed","Use stable, non-expiring URLs for long multi-connection downloads","Prefer CDNs known to consistently honor Range requests","Fall back to a single-connection (non-range) download if multi-connection repeatedly fails"],"tags":["http","android","file-download","socket","retry"],"backgroundTag":"http-error-response","analyzedSha":"6237a8cac174bcc916e4342b14ab1ab72a5768d4","analyzedAt":"2026-09-08T23:50:48.168Z","contentChangedAt":"2026-09-08T23:50:48.168Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}