{"record":{"id":"d27dbceb1ecf4243","repo":"lingochamp/FileDownloader","slug":"response-code-error-d-request-headers-s-r","errorCode":null,"errorMessage":"response code error: %d, \n request headers: %s \n response headers: %s","messagePattern":"response code error: (.+?), \n request headers: (.+?) \n response headers: (.+?)","errorType":"http","errorClass":"FileDownloadHttpException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/liulishuo/filedownloader/download/DownloadLaunchRunnable.java","lineNumber":571,"sourceCode":"\n        redirectedUrl = connectTask.getFinalRedirectedUrl();\n        if (acceptPartial || onlyFromBeginning) {\n\n\n            // update model\n            String fileName = null;\n            if (model.isPathAsDirectory()) {\n                // filename\n                fileName = FileDownloadUtils.findFilename(connection, model.getUrl());\n            }\n            isChunked = (totalLength == TOTAL_VALUE_IN_CHUNKED_RESOURCE);\n\n            // callback\n            statusCallback.onConnected(isResumeAvailableOnDB && acceptPartial,\n                    totalLength, newEtag, fileName);\n\n        } else {\n            throw new FileDownloadHttpException(code,\n                    requestHeader, connection.getResponseHeaderFields());\n        }\n    }\n\n    private void realDownloadWithSingleConnection(final long totalLength)\n            throws IOException, IllegalAccessException {\n\n        // connect\n        final ConnectionProfile profile;\n        if (!acceptPartial) {\n            model.setSoFar(0);\n            profile = ConnectionProfile.ConnectionProfileBuild\n                    .buildBeginToEndConnectionProfile(totalLength);\n        } else {\n            profile = ConnectionProfile.ConnectionProfileBuild\n                    .buildToEndConnectionProfile(model.getSoFar(), model.getSoFar(),\n                            totalLength - model.getSoFar());\n        }","sourceCodeStart":553,"sourceCodeEnd":589,"githubUrl":"https://github.com/lingochamp/FileDownloader/blob/6237a8cac174bcc916e4342b14ab1ab72a5768d4/library/src/main/java/com/liulishuo/filedownloader/download/DownloadLaunchRunnable.java#L553-L589","documentation":"FileDownloadHttpException is thrown by DownloadLaunchRunnable.handleTrialConnectResult when the server's HTTP response code is not acceptable for the trial/redirect connection (not 206 Partial Content or 200 OK). It carries the HTTP status code plus the full request and response headers so the developer can diagnose the server-side rejection. This is the library's way of surfacing that the remote server refused or mishandled the download request.","triggerScenarios":"The server responds to the download (or 'Accept-Ranges' trial) request with a status code other than HTTP 200/206 — e.g. 403 Forbidden, 404 Not Found, 416 Range Not Satisfiable, 5xx server errors, or a redirect loop — after FileDownloader has opened the HttpURLConnection.","commonSituations":"URL points to a page that returns 404/403 instead of a file; CDN or server rejects Range requests (416); expired signed URLs (S3 pre-signed links returning 403); server requiring auth cookies/headers not supplied; proxy/firewall returning HTML error pages.","solutions":["Log/inspect the request and response headers attached to the exception to see exactly how the server rejected the request","Verify the URL is still valid and returns the file directly (test with curl -I)","Check whether the server honors Range requests; disable resume/trial download (FileDownloadProperties isTrialCancelled or remove range headers) if it returns 416","Add required authentication headers/cookies to the request via FileDownloadListener addHeader or ConnectTask header setup","Retry on 5xx with backoff; fix server-side issue or use a mirror URL"],"exampleFix":"// before: downloading an expired pre-signed URL\nFileDownloader.getImpl().create(url).setPath(path).start();\n// after: refresh the URL and add required headers before downloading\nString freshUrl = refreshSignedUrl(url);\nFileDownloader.getImpl().create(freshUrl)\n    .addHeader(\"Authorization\", token)\n    .setPath(path)\n    .start();","handlingStrategy":"try-catch","validationCode":"HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();\nc.setRequestMethod(\"HEAD\");\nint code = c.getResponseCode();\nif (code != 200 && code != 206) throw new IllegalStateException(\"URL not downloadable, code=\" + code);","typeGuard":null,"tryCatchPattern":"try {\n    FileDownloader.getImpl().create(url).setPath(path).start(listener);\n} catch (FileDownloadHttpException e) {\n    // e.getCode(), e.getRequestHeader(), e.getResponseHeader() for diagnostics\n    Log.e(TAG, \"download failed with HTTP \" + ((FileDownloadHttpException) e).getCode(), e);\n}","preventionTips":["HEAD-check the URL before starting the download","Keep signed URLs refreshed and short-lived tokens valid for the download duration","Confirm the server supports Range requests if resuming is expected","Attach required auth headers to the FileDownloader request"],"tags":["http","android","file-download","http-error-response"],"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"}