请帮助我!如何在初始化期间在webview_flutter中设置Cookies?谢谢!
body: WebView
initialUrl: index_url,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller=webViewController;
},
javascriptChannels: <JavascriptChannel>[
_toasterJavascriptChannel(context),
].toSet(),
onPageFinished: (String url) {
print('Page finished loading: $url');
},
),
答案 0 :(得分:2)
您可以使用Tow方法设置cookie。
解决方案1。
使用webViewController.evaluateJavascript()
例如:
final Completer<WebViewController> _controller =
Completer<WebViewController>();
WebViewController _webViewController;
@override
Widget build(BuildContext context) {
_controller.future.then((controller) {
_webViewController = controller;
_webViewController.loadUrl(URL);
});
return Scaffold(
body: SafeArea(
child: Container(
child:
WebView(
debuggingEnabled: true,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
onPageFinished: (String value) {
setSession(sessionId, _webViewController);
},
),
),
),
);
}
void setSession(String sessionId, WebViewController webViewController) async {
if (Platform.isIOS) {
await webViewController.evaluateJavascript("document.cookie = 'ASP.NET_SessionId=$sessionId'");
} else {
await webViewController.evaluateJavascript('document.cookie = "ASP.NET_SessionId=$sessionId; path=/"');
}
}
,但是使用此方法,我们可以在WebView中的onPageFinished
之后设置cookie。仅在重新加载Web视图后,此解决方案才有效。这意味着在第一次构建时,此方法将向WebView添加cookie,并在第二次以后获得结果。
如果您希望WebView加载预设的Cookies,请检查下一个解决方案
解决方案2。。我们可以使用headers
在启动http请求时设置cookie。例如:
final Completer<WebViewController> _controller =
Completer<WebViewController>();
WebViewController _webViewController;
@override
Widget build(BuildContext context) {
_controller.future.then((controller) {
_webViewController = controller;
Map<String, String> header = {'Cookie': 'ASP.NET_SessionId=$sessionId'};
_webViewController.loadUrl(URL, headers: header);
});
return Scaffold(
body: SafeArea(
child: Container(
child:
WebView(
debuggingEnabled: true,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
),
),
),
);
}
这将使用给定的Cookie加载WebView。
答案 1 :(得分:1)
@deepakraj我一直在努力让其中之一工作起来没有运气。第二个是我最感兴趣的代码。这是我的代码。我正在模拟对后端的领先异步请求,该请求首先需要解决才能构建Webview(标头的源),这就是为什么我使用futureBuilder的原因。也许我在这里有一个明显的问题。
WebViewController _webViewController;
Future ssoRequestFuture;
@override
void initState() {
// TODO: implement initState
super.initState();
this.ssoRequestFuture = makeGetRequest();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<http.Response>(
future: this.ssoRequestFuture,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Not connected');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('waiting');
case ConnectionState.done:
var cookieStr = snapshot.data.headers['set-cookie'];
List<String> cookies = cookieStr.split(","); // I am not interested n the first cookie
Cookie ssoCookie = Cookie.fromSetCookieValue(cookies[1]);
Map<String, String> header = {'Cookie': '$ssoCookie'};
// clear cookies was a suggestion ??
CookieManager cookieManager = CookieManager();
cookieManager.clearCookies();
return Scaffold(
body: Builder(builder: (BuildContext context) {
_controller.future.then((controller) {
_webViewController = controller;
_webViewController.loadUrl('http://192.168.86.28:4205', headers: header);
});
return WebView(
debuggingEnabled: true,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
gestureNavigationEnabled: true,
);
}),
);
default:
return Text('Default');
}
}
);
}
答案 2 :(得分:0)
{@ {1}}抖动对我有以下作用:
1.22.1
请注意,WebView(
onWebViewCreated: (controller) {
controller.loadUrl(
"https://mypage.de/",
headers: {"Cookie": "mycookie=true"},
);
},
)
中没有冒号,如其他答案所示。
答案 3 :(得分:-1)
您可以将 webview_cookie_manager 包与 webview_flutter
一起使用。它有效:
await cookieManager.setCookies([
Cookie('cookieName', 'cookieValue')
..domain = 'youtube.com'
..expires = DateTime.now().add(Duration(days: 10))
..httpOnly = false
]);