如何在android webview中拦截POST数据
我有一个包含 webview 的 Android 应用程序。它需要允许用户在网页上填写表单,然后在用户单击表单上的提交
后更改表单的数据。该表单将使用 POST 请求方法。
所以我的问题是,如何从表单中拦截 POST 数据,更改其值,然后发送它?
例如:如果有这样的 Web 表单...
<form action="http://www.example.com/do.php" method="post">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="submit" />
</form>
如果用户输入姓名= 史蒂夫和电子邮件 = [email protected] 在表单中,我想将值更改为 name = bob 和 email = [email protected] 并将新的 POST 发送到 http://www.example.com/do.php
。
感谢您的帮助!
I have an android app that consists of a webview. It needs to allow users to fill in a form on a webpage and then change the data of the form after the user has clicked submit
on the form. The form will use the POST request method.
So my question is, how can I intercept the POST data from the form, change it's values, then send it along?
For example: If there's a web form like this...
<form action="http://www.example.com/do.php" method="post">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="submit" />
</form>
If the user enters name = Steve and email = [email protected] in the form, I want to change the values to name = bob and email = [email protected] in the android app and have the new POST be sent to http://www.example.com/do.php
.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你熟悉 JavaScript,我建议你使用 JavaScript。我认为这样更方便、更容易。 本教程将告诉您如何在 WebView 中使用 JavaScript。
If you are familiar with JavaScript, I would suggest you use JavaScript. I think it's more convenient and easy. This tour tells you how to use JavaScript in a WebView.
如果您使用 postUrl() 方法提交表单,那么您可以像这样覆盖 WebView 对象中的 postUrl 方法。
if you are submitting the form using postUrl() method then you can override the postUrl method in your WebView object like this.
我编写了一个带有特殊 WebViewClient 的库,它提供了修改后的shouldOverrideUrlLoading,您可以在其中访问发布数据。
https://github.com/KonstantinSchubert/request_data_webviewclient
不幸的是,我的实现适用于 XMLHttpRequest (AJAX) 请求仅有的。但其他人已经起草了如何对表单数据执行此操作: https://github.com/ KeejOow/android-post-webview
在这两个存储库之间,您应该能够找到答案:)
I wrote a library with a special WebViewClient that offers a modified
shouldOverrideUrlLoading
where you can have access to post data.https://github.com/KonstantinSchubert/request_data_webviewclient
Unfortunately, my implementation works for XMLHttpRequest (AJAX) requests only. But somebody else drafted already how this would be done for form data: https://github.com/KeejOow/android-post-webview
Between the two repositories, you should be able to find your answer :)
我喜欢 public void postUrl(String url, byte[] postData) 的建议,但不幸的是它对我不起作用。
我的解决方案只是拦截 POST 请求:
代码摘录&amp;其他想法:https://stackoverflow.com/a/9493323/2162226
I liked the suggestion for public void postUrl(String url, byte[] postData) , but unfortunately it did not work for me.
My solution for just intercepting the POST request:
Code excerpt & additional thoughts here: https://stackoverflow.com/a/9493323/2162226
覆盖
WebView
中的onPageStarted
回调overriding
onPageStarted
callback in yourWebView
我认为您可以使用 WebViewClient 的
shouldInterceptRequest(WebView view, String url)
方法,但 API 11 之后才支持它。I think you can use
shouldInterceptRequest(WebView view, String url)
method of WebViewClient, but it is supported in the API's later than 11.