flutter - 我如何处理 webview 中发出的请求的响应

问题描述 投票:0回答:1

有没有办法处理在flutter上的webview中发出的请求的json响应?所以场景是:

  • 网站正在
    webview
    内发出请求。所以我不会手动发送任何请求。基本上网站发送一个http请求到
    /api/v1/fetch
  • 当返回网站发送的请求的响应时,我需要处理 json 响应。

我尝试使用

useShouldInterceptRequest
但无法实现。

我正在apwebview包中使用flutter: https://pub.dev/packages/flutter_inappwebview

flutter webview
1个回答
0
投票

你读过这个包的文档吗?

您可以阅读以下内容:https://inappwebview.dev/docs/webview/in-app-weview-settings/#useshouldinterceptajxrequest

示例实现是(不是测试器):

InAppWebView(
                        initialUrl: Uri.https(authState.companyData.baseUrl,
                                '/administrator/Web.do')
                            .toString(),
                        initialOptions: InAppWebViewGroupOptions(
                            crossPlatform: InAppWebViewOptions(
                                debuggingEnabled: true,
                                useShouldInterceptAjaxRequest: true),
                            ios: IOSInAppWebViewOptions(
                                sharedCookiesEnabled: true),
                            ),
                        onWebViewCreated: (InAppWebViewController controller) {
                          webView = controller;
                        },
                        onLoadStop: (controller, url) async {
                          if (url.toLowerCase().endsWith('logout.do')) {
                            BlocProvider.of<AdminBloc>(context)
                                .add(AdminPageLogout());
                            // BlocProvider.of<AuthenticationBloc>(context)
                            //     .add(AuthenticationLoggedOut());
                            // BlocProvider.of<AuthenticationBloc>(context)
                            //     .add(AuthenticationStarted());
                          }
                          webView.canGoBack().then((value) {
                            setState(() {
                              canGoBack = value;
                            });
                          });
                          webView.canGoForward().then((value) {
                            setState(() {
                              canGoForward = value;
                            });
                          });
                        },
                        onProgressChanged: (controller, progress) {
                          setState(() {
                            loadingProgress = progress / 100;
                          });
                        },
                        shouldInterceptAjaxRequest:
                            (InAppWebViewController controller,
                                AjaxRequest ajaxRequest) async {
                          print(ajaxRequest.url);
                          ajaxRequest.headers.setRequestHeader("Cookie", authState.setCookie);
                          return ajaxRequest;
                        },
                        onAjaxReadyStateChange:
                            (InAppWebViewController controller,
                                AjaxRequest ajaxRequest) async {
                          print(ajaxRequest.status);
                          return AjaxRequestAction.PROCEED;
                        },
                        onAjaxProgress: (InAppWebViewController controller,
                            AjaxRequest ajaxRequest) async {
                          print(ajaxRequest.status);
                          return AjaxRequestAction.PROCEED;
                        },
                      )

请参阅

useShouldInterceptAjaxRequest: true
激活ajax请求拦截并参考:

shouldInterceptAjaxRequest:
                            (InAppWebViewController controller,
                                AjaxRequest ajaxRequest) async {
                          print(ajaxRequest.url);
                          ajaxRequest.headers.setRequestHeader("Cookie", authState.setCookie);
                          return ajaxRequest;
                        },

处理拦截。

另请参阅:在 Flutter webview 中拦截 AJAX 适用于 iOS,但不适用于 Android

© www.soinside.com 2019 - 2024. All rights reserved.