Android DownloadManager failed downloading with reason ERROR_TOO_MANY_REDIRECTS, how can I fix this issue for targeting API level 31 or higher?

huangapple go评论55阅读模式
英文:

Android DownloadManager failed downloading with reason ERROR_TOO_MANY_REDIRECTS, how can I fix this issue for targeting API level 31 or higher?

问题

使用android.app.DownloadManager下载文件,但有些文件下载失败,原因是ERROR_TOO_MANY_REDIRECTS。

我从下面的代码中获取了失败的原因:

val reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
val reason = cursor.getInt(reasonIndex)

// public final static int ERROR_TOO_MANY_REDIRECTS = 1005;

// 我得到了相同的原因1005

是否有办法增加maxRedirect以及默认的重定向次数是多少?

如何解决这个过多重定向的问题?

我目前使用类似以下的代码:

val downloadManager: DownloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

fun downloadFile(url: String, path: String, title: String? = null): Long {
    val downloadUri = Uri.parse(url)
    var downloadId: Long
    try {
        val request = DownloadManager.Request(downloadUri).apply {
            setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false)
            .setTitle(title ?: "")
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)
            .setDestinationUri(Uri.fromFile(File(path)))

        }
        downloadId = downloadManager?.enqueue(request) ?: -1
        scanMedia(path)
    } catch (exception: Exception) {
        downloadId = -1
    }
    return downloadId
}

我想要从android.app.DownloadManager下载文件,即使URL重定向了10次、20次或30次。

英文:

I am using android.app.DownloadManager for downloading file, but few files failed download, with reason ERROR_TOO_MANY_REDIRECTS,

I got the reason to failed from below code:

val reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
val reason = cursor.getInt(reasonIndex)

//public final static int ERROR_TOO_MANY_REDIRECTS = 1005;

// I got same reason 1005

Is there any way to increase maxRedirect and what is default redirect count?

How can I fixed this too many redirect issue?

Currently I am using similar to below code:

val downloadManager: DownloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

fun downloadFile(url: String, path: String, title: String? = null): Long {
        val downloadUri = Uri.parse(url)
        var downloadId: Long
        try {
            val request = DownloadManager.Request(downloadUri).apply {
                setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or     DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false)
                    .setTitle(title ?: "")
                    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)
                    .setDestinationUri(Uri.fromFile(File(path)))

            }
            downloadId = downloadManager?.enqueue(request) ?: -1
            scanMedia(path)
        } catch (exception: Exception) {
            downloadId = -1
        }
        return downloadId
}

I want to download file from android.app.DownloadManager even if the url redirect to more then 10, 20 or 30 times

答案1

得分: 0

为了解决这个问题,我们首先需要获取重定向的URL,并将最终的重定向URL提供给下载管理器进行下载。这是解决这个问题的方法。

@Throws(IOException::class)
suspend fun getFinalURL1(url: String, totalTimeOut: Long): String {
    return withContext(Dispatchers.IO) {
        if (totalTimeOut < System.currentTimeMillis()) {
            return@withContext url
        }

        delay(10)

        val obj = URL(url)
        val con: HttpURLConnection = obj.openConnection() as HttpURLConnection
        con.instanceFollowRedirects = false
        con.connect()
        con.inputStream
        if (con.responseCode == HttpURLConnection.HTTP_MOVED_PERM || con.responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
            var redirectUrl: String? = con.getHeaderField("Location")

            if (redirectUrl == null) {
                return@withContext url
            } else if (redirectUrl.startsWith("/")) {
                redirectUrl = obj.protocol.toString() + "://" + obj.host + redirectUrl
            }

            if (!redirectUrl.startsWith(startsWith, true)) {
                return@withContext url
            }

            return@withContext getFinalURL1(redirectUrl, totalTimeOut)
        }
        return@withContext url
    }
}
英文:

To solve this issue, we have to first get the redirected url and give the final redirected url to donwnload manager to download.
This is the way to solve this issue.

@Throws(IOException::class)
suspend fun getFinalURL1(url: String, totalTimeOut: Long): String {
    return withContext(Dispatchers.IO) {
        if (totalTimeOut &lt; System.currentTimeMillis()) {
            return@withContext url
        }

        delay(10)

        val obj = URL(url)
        val con: HttpURLConnection = obj.openConnection() as HttpURLConnection
        con.instanceFollowRedirects = false
        con.connect()
        con.inputStream
        if (con.responseCode == HttpURLConnection.HTTP_MOVED_PERM || con.responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
            var redirectUrl: String? = con.getHeaderField(&quot;Location&quot;)

            if (redirectUrl == null) {
                return@withContext url
            } else if (redirectUrl.startsWith(&quot;/&quot;)) {
                redirectUrl = obj.protocol.toString() + &quot;://&quot; + obj.host + redirectUrl
            }

            if (!redirectUrl.startsWith(startsWith, true)) {
                return@withContext url
            }

            return@withContext getFinalURL1(redirectUrl, totalTimeOut)
        }
        return@withContext url
    }
}

huangapple
  • 本文由 发表于 2023年7月10日 15:47:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651695-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定