android flutter_inappwebview和flutter_downloader -不确定我做错了什么?

o7jaxewo  于 2023-01-24  发布在  Android
关注(0)|答案(1)|浏览(238)

前期信息:

Language: Flutter/Dart
Flutter Packages: flutter_inappwebview, flutter_downloader

我使用Flutter为Android构建的移动的应用程序遇到了问题。该应用程序的一个功能是能够下载与PDF文件关联的链接。这些文件位于该应用程序已经访问的服务器上。用户已经通过该应用程序登录,因此身份验证已经建立。当点击包含PDF的URL时,它会执行重定向和主页(一个indexiderphp)被返回并下载代替。我不知道这是如何发生的,但我有想法。
1.我需要有下载发生在当前的网页视图(在同一个会话和窗口)。
1.禁用URL重定向。
1.弄清楚为什么链接被重定向进行身份验证(假设它被重定向到主页)。
任何想法或帮助将不胜感激。很高兴提供更多的信息以及。
在主省道中:

onDownloadStart: (controller, url) async {
                print("onDownloadStart $url");
                final taskId = await FlutterDownloader.enqueue(
                  headers: {'My-Custom-Header': 'custom_value=564hgf34'},
                  url: url,
                  savedDir: (await getExternalStorageDirectory()).path,
                  showNotification: true, // show download progress in status bar (for Android)
                  openFileFromNotification: true, // click on notification to open downloaded file (for Android)
                );
                return taskId;
              },

记录:

I/flutter (28552): onDownloadStart https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf
W/WM-WorkSpec(28552): Backoff delay duration less than minimum value
D/DownloadWorker(28552): DownloadWorker{url=https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf,filename=null,savedDir=/storage/emulated/0/Android/data/com.coffeepaulconsulting.hjhvtconline/files,header={"My-Custom-Header": "custom_value=564hgf34"},isResume=false
D/DownloadWorker(28552): Update notification: {notificationId: 1, title: https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf, status: 2, progress: 0}
D/DownloadWorker(28552): Open connection to https://hjhvtc.online/pluginfile.php/6723/mod_resource/content/1/Automotive%20industry%20jobs.pdf
D/DownloadWorker(28552): Headers = {"My-Custom-Header": "custom_value=564hgf34"}
D/DownloadWorker(28552): Response with redirection code
D/DownloadWorker(28552): Location = https://hjhvtc.online/login/index.php
D/DownloadWorker(28552): New url: https://hjhvtc.online/login/index.php
D/DownloadWorker(28552): Open connection to https://hjhvtc.online/login/index.php
D/DownloadWorker(28552): Headers = {"My-Custom-Header": "custom_value=564hgf34"}
V/InputMethodManager(28552): b/117267690: Failed to get fallback IMM with expected displayId=197 actual IMM#displayId=0 view=com.pichillilorenzo.flutter_inappwebview.InAppWebView.InAppWebView{4592e22 VFEDHVCL. ......ID 0,0-1080,1997}
D/DownloadWorker(28552): Content-Type = text/html; charset=utf-8
D/DownloadWorker(28552): Content-Length = -1
D/DownloadWorker(28552): Charset = UTF-8
D/DownloadWorker(28552): Content-Disposition = null
D/DownloadWorker(28552): fileName = index.php
D/DownloadWorker(28552): Update too frequently!!!!, this should be dropped
D/DownloadWorker(28552): There's no application that can open the file /storage/emulated/0/Android/data/com.coffeepaulconsulting.hjhvtconline/files/index.php
D/DownloadWorker(28552): Update too frequently!!!!, but it is the final update, we should sleep a second to ensure the update call can be processed
D/DownloadWorker(28552): Update notification: {notificationId: 1, title: index.php, status: 3, progress: 100}
D/DownloadWorker(28552): File downloaded
I/WM-WorkerWrapper(28552): Worker result SUCCESS for Work [ id=633dd93d-99e3-46e2-937d-a4782d62a569, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]
66bbxpm5

66bbxpm51#

经过几次尝试,我终于找到了解决方案,DownloadWorker被重定向到登录页面,因为尽管用户在InAppWebView中进行了身份验证,但flutter下载器似乎运行在一个单独的上下文中,因此用户没有经过身份验证。
为了维护用户会话,我创建了一个变量:

// Store cookies to save user session for download
  String cookiesString = '';

然后,我创建了updateCookies函数,它从CookieManager中检索cookie并更新cookiesString变量:

Future<void> updateCookies(Uri url) async {
    List<Cookie> cookies = await CookieManager().getCookies(url: url);
    cookiesString = '';
    for (Cookie cookie in cookies) {
      cookiesString += '${cookie.name}=${cookie.value};';
    }
    print(cookiesString);
  }

然后,我在InAppWebView的onLoadStop事件侦听器中调用updateCookies

onLoadStop: (controller, url) async {
    pullToRefreshController.endRefreshing();

    if (url != null) {
        await updateCookies(url);
    }

    setState(() {
        this.url = url.toString();
        urlController.text = this.url;
    });
},

最后,我将cookie传递给onDownloadStart事件侦听器中FlutterDownloader的头:

await FlutterDownloader.enqueue(
                    headers: {
                      HttpHeaders.authorizationHeader: 'Basic ' +
                          authToken,
                      HttpHeaders.connectionHeader: 'keep-alive',
                      HttpHeaders.cookieHeader: cookiesString,
                    },

相关问题