如何在android webview中使用代码发送POST数据 [英] How to send POST data with code in an android webview

查看:84
本文介绍了如何在android webview中使用代码发送POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 WebWiew 组成的 android 应用程序,我需要使用代码自动登录到一个站点.我试过使用 postUrl() 并且它似乎有效......但仅限于某些网站.

I have an android application that consists of a WebWiew and I need to login to a site automatically using code. I've tried using postUrl() and it seems to work... but only on some sites.

这是我正在使用的代码:

Here's the code I'm using:

public class webviewActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webview = new WebView(this);
        setContentView(webview);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webview.setWebViewClient(new WebViewClient());

        String postData = "login_email=myEmail@gmail.com&login_password=myPassword";
        webview.postUrl("https://www.dropbox.com/login", EncodingUtils.getBytes(postData, "utf-8"));
    }
}

这对 dropbox.com 很好,但其他网站,如 google.com、facebook.com 等,只加载登录页面或给出错误(google.com 给出一个错误说我需要启用cookies).

This Works great for dropbox.com, but other sites like google.com, facebook.com, etc. just load the login page or give an error (google.com gives an error saying I need to enable cookies).

现在我只是手动处理发布数据;查看站点的登录表单并将名称/值字段放在我的代码中的 postData 中.在 google 等网站上,登录表单有许多隐藏字段,我也将这些字段添加到 postData.

Right now I'm just going the post data by hand; looking at the login form for the site and putting the name/value fields in the postData in my code. On sites like google, the login form has many hidden fields and I've been adding those to the postData also.

如果有人可以让我知道我做错了什么,请告诉我,我对此很困惑.

If anyone could give me any idea of something I'm doing wrong please let me know, I'm pretty confused about this.

推荐答案

尝试用 "BASE64" 替换 "utf-8"(在第二个参数中).

Try replacing "utf-8" (in the 2nd param) with "BASE64".

这篇关于如何在android webview中使用代码发送POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆