How do I write a webview to automatically sign into my yahoo account?

369 views Asked by At

I am writing a test webview app, in which I intend to open my yahoo mail the moment I launch the app view. It will be a webview, where I want to hardcode my username and password. I know yahoo uses get method instead of post. Is there a way I can achieve this? Here's my code so far:

Webview webview = (WebView) getView().findViewById(R.id.mywebview);
webview.setBackgroundColor(0);
            webview.getSettings().setJavaScriptEnabled(true);

            webview.setWebViewClient(new webClient());
            String url = "https://www.yahoomail.com";
            webview.loadUrl(url);

private class webClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.setVisibility(View.VISIBLE);

        }
        @Override
        public void onPageFinished(WebView view, String url) {

            final Animation fade = new AlphaAnimation(0.0f, 1.0f);
            fade.setDuration(200);
            view.startAnimation(fade);

            view.setVisibility(View.VISIBLE);

        }
        public void animate(final WebView view) {
            final Animation anim = AnimationUtils.loadAnimation(getActivity(),
                    R.anim.slide_in_from_left);
            view.startAnimation(anim);
        }
        @Override
        public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
            Toast.makeText(view.getContext(), "Authentication Error", Toast.LENGTH_LONG).show();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setCookie("[email protected]", "yahoopass");
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public void onLoadResource( WebView view, String url ){

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            return super.shouldOverrideUrlLoading(view, url);

        }

        @Override
        public void onReceivedHttpAuthRequest( WebView view, final HttpAuthHandler handler, final String host, final String realm ){

            handler.proceed("[email protected]", "yahoopass");

        }

    }
1

There are 1 answers

2
ByteHamster On

http://developer.android.com/reference/android/webkit/WebView.html

loadUrl(String url)
Loads the given URL.

postUrl(String url, byte[] postData)
Loads the URL with postData using "POST" method into this WebView.

So instead of using:

String url = "https://www.yahoomail.com";
webview.loadUrl(url);

use:

String url = "http://example.com/somepage.php";
String postData = "postvar=value&postvar2=value2";
webView.postUrl(url, EncodingUtils.getBytes(postData, "base64"));

You can extract the vars you need for Yahoo from the login page.