有没有办法处理在flutter上的webview中发出的请求的json响应?所以场景是:
webview
内发出请求。所以我不会手动发送任何请求。基本上网站发送一个http请求到/api/v1/fetch
我尝试使用
useShouldInterceptRequest
但无法实现。
我正在apwebview包中使用flutter: https://pub.dev/packages/flutter_inappwebview
你读过这个包的文档吗?
您可以阅读以下内容: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;
},
处理拦截。