1

How is it possible to login programmatically on to a website ?

Example: The User type in his login data in EditText and click on the login Button. Then opens a WebView, where he is logged in.

I tried to work with these code examples but:

How to connect via HTTPS using Jsoup? - i got Errors

How to log in to an HTTPS website with Jsoup? - i didnt got Errors but i cant login

Jsoup HTTPS connecting - it dont work i got Errors

After i used this, i tried this

class JsoupAsyncIserv extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        //HTML Parsen
        try {


            Connection.Response login = Jsoup.connect("https://lmg-nhs.de/idesk")
                    .data("login_act", "MyUsername")
                    .data("login_pwd", "MyPW")
                    .method(Connection.Method.POST)
                    .execute();

            Document document = Jsoup.connect("https://lmg-nhs.de/idesk")
                    .cookies(login.cookies())
                    .get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {


    }
}

It works, but i cant login.

private class IServAsyncTask extends AsyncTask<Void, Void, Void>{

        private String resp;
        protected String doInBackground(String... params) {
            Connection.Response res = null;
            try {
                res = Jsoup.connect("https://lmg-nhs.de")
                        .data("login_act", "myUsername")
                        .data("login_pwd", "myPW")
                        .data("submit", "Anmelden")
                        .method(Connection.Method.POST)
                        .execute();

            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                Document doc = res.parse();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String sessionId = res.cookie("PHPSESSID");

            try {
                Document doc2 = Jsoup.connect("https://lmg-nhs.de")
                        .cookie("PHPSESSID", sessionId)
                        .get();
            } catch (IOException e) {
                e.printStackTrace();

            }

            return resp;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            return null;
        }
    }

If you can, try to explain it at an or my example, because beginners (like me) can easier learn it this way.

Community
  • 1
  • 1
Excel1
  • 557
  • 1
  • 7
  • 20
  • Using Chrome open dev tools and open the Network tab, mark to Preserve log, clear cookies/cache, then log into the page an inspect the request and responses. You should try to set all needed header fields (and setting a user-agent is usually a good idea). A general example is often too general (but they exist: http://stackoverflow.com/documentation/jsoup/4631/logging-into-websites-with-jsoup) and without having an account we can't provide a specific solution. – Frederic Klein Sep 26 '16 at 09:34
  • A better way for your login might be: open a webview (without displaying it), fill and submit the login form using javascript, then display the webview. – Frederic Klein Sep 26 '16 at 09:48
  • 1
    Are sure `String doInBackground(String... params)` gets called and not `Void doInBackground(Void... voids)`? – Stephan Sep 26 '16 at 10:07
  • @F.Klein I know that help is very difficulty but thank you for trying it! I tried it with the first example form [http://stackoverflow.com/documentation/jsoup/4631/logging-into-websites-with-jsoup] but i cant login. I need the login to show a logged in WebView and to download something without WebView. If you have any other ideas or helps then let it hear me. All can help me ! – Excel1 Sep 26 '16 at 16:02
  • @Stephan i found only a Jsoup Example with Asynctask in this way `Void doInBackground(Void... voids)` I tried to work with this example but i have no ideas to fix it. If you have a better way to do this, let it hear me. – Excel1 Sep 26 '16 at 16:05
  • @F.Klein do you maybe know a other way to loggin in ? Maybe with the WebView ? – Excel1 Sep 28 '16 at 13:07

1 Answers1

2

Try this:

Document doc2 = Jsoup.connect("https://lmg-nhs.de")
                    .cookie(res.cookies())
                    .get();

instead of

Document doc2 = Jsoup.connect("https://lmg-nhs.de")
                    .cookie("PHPSESSID", sessionId)
                    .get();

See also:

In this example, we will log into the GitHub website by using the FormElement class.

// # Constants used in this example
final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"; 
final String LOGIN_FORM_URL = "https://github.com/login";
final String USERNAME = "yourUsername";  
final String PASSWORD = "yourPassword";  

// # Go to login page
Connection.Response loginFormResponse = Jsoup.connect(LOGIN_FORM_URL)
                                             .method(Connection.Method.GET)
                                             .userAgent(USER_AGENT)
                                             .execute();  

// # Fill the login form
// ## Find the form first...
FormElement loginForm = (FormElement)loginFormResponse.parse()
                                         .select("div#login > form").first();
checkElement("Login Form", loginForm);

// ## ... then "type" the username ...
Element loginField = loginForm.select("#login_field").first();
checkElement("Login Field", loginField);
loginField.val(USERNAME);

// ## ... and "type" the password
Element passwordField = loginForm.select("#password").first();
checkElement("Password Field", passwordField);
passwordField.val(PASSWORD);        


// # Now send the form for login
Connection.Response loginActionResponse = loginForm.submit()
         .cookies(loginFormResponse.cookies())
         .userAgent(USER_AGENT)  
         .execute();

System.out.println(loginActionResponse.parse().html());

public static void checkElement(String name, Element elem) {
    if (elem == null) {
        throw new RuntimeException("Unable to find " + name);
    }
}

All the form data is handled by the FormElement class for us (even the form method detection). A ready made Connection is built when invoking the FormElement#submit method. All we have to do is to complete this connection with addional headers (cookies, user-agent etc) and execute it.

Graham
  • 7,431
  • 18
  • 59
  • 84
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • Thank you for your answer. I tried it but `res` from `.cookie(res.cookies())` got not defined. I defined it `Connection.Response res = null;` but Android Studio try it do surround it with `res != null` and i got an error. – Excel1 Sep 26 '16 at 15:50
  • @Excel1 Rewrite your code to use only one `try` block and not three. – Stephan Sep 26 '16 at 16:31
  • i tried but it didnt worked. `.cookie(res.cookies())` _cannot be applied_ If you have a code example maybe i can try it and learn it. Thank you for help – Excel1 Sep 27 '16 at 08:27
  • @Excel1 Please see the link to the documentation (http://stackoverflow.com/documentation/jsoup/4631/logging-into-websites-with-jsoup/23695/logging-with-formelement#t=201609261108016427445) in my post. – Stephan Sep 27 '16 at 08:47
  • i need only a way to log successfully in on this page. Jsoup was the "best" method i found but i dont know it with and without jsoup. – Excel1 Sep 27 '16 at 14:30
  • And do i need it to put this in Async Task ? And if i need to do this then please tell me how – Excel1 Sep 27 '16 at 14:36
  • http://pastebin.com/iDrCRkhw there i did it on 2 Ways... problem: `String authToken = loginDoc.select("#login > form > div:nth-child(1) > input[type=\"hidden\"]:nth-child(2)") .first() .attr("value");` because: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jsoup.nodes.Element.attr(java.lang.String)' on a null object reference – Excel1 Sep 27 '16 at 15:24