Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

http - How to send POST data with code in an android webview

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"));
    }
}

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).

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...