31

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.

javajavajavajavajava
  • 1,223
  • 2
  • 12
  • 20

3 Answers3

15

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

an00b
  • 11,338
  • 13
  • 64
  • 101
  • 1
    Hi an00b. Thanks for the answer! I believe I have tried this with no luck. I'm no longer working on this specific problem, I decided to go a separate route. I'll vote you up anyway, because I really appreciate the answer. – javajavajavajavajava Dec 13 '11 at 20:33
  • "BASE64" is no character set, so this may work (depending on the postData) but it may as well fail. The correct way is to encode the postData to be "application/x-www-form-urlencoded" encoded, which can be done with URLEncoder.encode(postData, "UTF-8"); – raudi May 15 '15 at 11:10
  • 1
    raudi can u put an answer with a snippet useing "application/x-www-form-urlencoded" – D4rWiNS Nov 13 '15 at 10:39
11
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    WebView webView = new WebView(this);

    setContentView(webView);

    String url = "http://example.com/somepage.php";
    String postData = "postvar=value&postvar2=value2";

    webView.postUrl(url, EncodingUtils.getBytes(postData, "base64"));
}
filippo
  • 839
  • 2
  • 13
  • 25
Laxman
  • 111
  • 1
  • 3
  • 15
    I read the source code of EncodingUtils.java in Android [HERE](http://grepcode.com/file/repo1.maven.org/maven2/org.apache.httpcomponents/httpcore/4.4/org/apache/http/util/EncodingUtils.java#95) `EncodingUtils.getBytes(postData, "base64")` as same as `postData.getBytes()`. Just use `postData.getBytes()` instead of EncodingUtils. (If you need migrate to Android M (API 23) with no EncodingUtils) – Johnny Nov 26 '15 at 11:07
  • You saved my day dude. – NinjaCoder Jan 07 '16 at 00:40
2
WebView myWebView = (WebView) findViewById(R.id.webview);

String url="http://www.example.org/login";

String postData=
                "username="+URLEncoder.encode("abc","UTF8")+
                "&password="+URLEncoder.encode("***", "UTF-8");

myWebView.postUrl(url,postData.getBytes());
pruthwiraj.kadam
  • 1,033
  • 10
  • 11