如何在 Flutter WebView 中将数据发布到 URL [英] How to Post Data to URL in Flutter WebView
问题描述
我想将一些数据发布到 Flutter WebView 中的 URL 正文.那么,我该怎么做?
I want to post some data to the URL body in Flutter WebView. So, how can I do it?
推荐答案
webview_flutter
目前没有发送 post 请求的方法.但是,您可以尝试我的 flutter_inappwebview 插件.支持POST请求!
webview_flutter
doesn't have a method to send post requests at this moment.
However, you can try my flutter_inappwebview plugin. It supports POST requests!
使用flutter_inappwebview的当前最新版本5.0.5+3
的简单示例 插件是:
A simple example using the current latest version 5.0.5+3
of the flutter_inappwebview plugin is:
var postData = Uint8List.fromList(utf8.encode("firstname=Foo&lastname=Bar"));
controller.postUrl(url: Uri.parse("https://example.com/my-post-endpoint"), postData: postData);
其中 postData
是 x-www-form-urlencoded
格式的请求正文.
where the postData
is the Body of the request in x-www-form-urlencoded
format.
例如,如果您有一个 PHP 服务器,您可以像往常一样访问 firstname
和 lastname
值,即 $_POST['firstname']
和 $_POST['lastname']
.
For example, if you have a PHP server, you can access the firstname
and lastname
values as you normally would do, that is $_POST['firstname']
and $_POST['lastname']
.
您还可以使用初始 POST 请求初始化 InAppWebView
小部件,如下所示:
You can also initialize the InAppWebView
widget with an initial POST request like this:
child: InAppWebView(
initialUrlRequest: URLRequest(
url: Uri.parse("https://example.com/my-post-endpoint"),
method: 'POST',
body: Uint8List.fromList(utf8.encode("firstname=Foo&lastname=Bar")),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
),
onWebViewCreated: (controller) {
},
),
这篇关于如何在 Flutter WebView 中将数据发布到 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!