簡體   English   中英

當 PDF 受 cookie 保護時,如何從 WebView 打開 PDF?

[英]How to open a PDF from WebView when PDF is secured by a cookie?

我正在嘗試從我的 WebView 下載並打開一個 PDF 文件。 我嘗試從 WebView 檢索 cookie,並在 DownloadManager.Request 上設置 cookie。 當我的 BroadCastReceiver 被觸發時,下載狀態表明它已失敗。

this.webView?.apply {
            settings.domStorageEnabled = true
            settings.javaScriptEnabled = true

            setDownloadListener { url, _, _, mimetype, _ ->
                Log.w("downloading file", url)
                val downloadUri = Uri.parse(url)
                val downloadRequest = DownloadManager.Request(downloadUri).apply {
                    setTitle("Title")
                    setDescription("Downloading file")
                    Log.w("path", "${downloadUri.lastPathSegment}")
                    setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, downloadUri.lastPathSegment)
                    val cookie = getCookie("https://www.example.com", "example_cookie_name")
                    Log.w("cookie", "${cookie}")
                    addRequestHeader("Cookie", cookie)
                }

                val manager: DownloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
                val downloadId = manager.enqueue(downloadRequest)

                registerReceiver(
                    object: BroadcastReceiver() {
                        override fun onReceive(context: Context?, intent: Intent?) {
                            Log.w("onReceive", "${intent?.action}")
                            if (intent !== null && DownloadManager.ACTION_DOWNLOAD_COMPLETE == intent.action) {
                                val cursor = manager.query(DownloadManager.Query().apply{ setFilterById(downloadId) })

                                if (cursor.moveToFirst()) {
                                    Log.w("onReceive", "cursor moved")
                                    val downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
                                    val downloadLocalUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
                                    val downloadMimeType = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE))
                                    Log.w("onReceive", "$downloadStatus $downloadLocalUri $downloadMimeType")
                                    if (downloadStatus == DownloadManager.STATUS_SUCCESSFUL && downloadLocalUri !== null) {
                                        val viewIntent = Intent(Intent.ACTION_VIEW).apply {
                                            this.setDataAndType(Uri.parse(downloadLocalUri), downloadMimeType)
                                        }
                                        if (viewIntent.resolveActivity(packageManager) !== null) {
                                            startActivity(
                                                Intent.createChooser(viewIntent, "Choose app")
                                            )
                                        } else {
                                            Toast.makeText(
                                                context,
                                                "No app available that can open file",
                                                Toast.LENGTH_SHORT
                                            )
                                                .show()
                                        }
                                    }
                                }


                            }
                        }
                    },
                    IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
                )
            }
}

通過登錄此代碼,我可以確認我正在從 WebView 中獲取 cookie。

有沒有更好的方法來處理這個問題? 如果沒有,我如何確定下載失敗的原因? 另外,我做錯了什么?

理想情況下,我還可以下載該文件,並確保在用戶查看完 PDF 后將其刪除。

你可以在加載 webview 的內容之前嘗試這樣的事情。

fun setCookies() {
       val cookieManager = CookieManager.getInstance()
       val cookieString = "your_cookie_here"
       cookieManager.setCookie("domain_name_of_the_dowload_url", cookieString)
       cookieManager.setAcceptThirdPartyCookies(your_webview_here, true)
}

事實證明,我檢索 cookie 的方式是錯誤的,它最終剝離了密鑰。 為了將來參考,在下載請求上設置 cookie 非常簡單,使用:

addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM