简体   繁体   中英

Authorization in osticket using Java in Android

Well, main idea is to get cookies and then parse info from https://support.tltsu.ru/ . Well, we se form on page https://support.tltsu.ru/login.php , where we need to input "login" and "password" by using POST.

<form action="login.php" method="post" id="clientLogin" onsubmit="return C1heckFields()">
<!--?php csrf_token(); ?-->
<strong></strong>
<br>

<div>
    <label for="email">Логин:</label>
<input id="myl" type="text" name="login" size="16" value="">
</div>

<div>
    <label for="ticketno">Пароль:</label>
    <input id="ticketno" type="password" name="password" size="16" value="">
</div>
<p>
    <input class="btn" type="submit" value="Войти">
</p>  
</form>

And I also see that we need to input PHPSESSID in cookies. Good. So the plan is to firstly get started cookies, then input them in our POST request. Dont know how but after that our cookies (PHPSESSID without changing) becomes valid and even conected to our account. So, you can say and what else you wnat? Then I can tell you - authorization just doesn't work for me in android. Im using Jsoup - html framework and here my code:

    class ApplicationsAuth extends AsyncTask<Void, Void, Void> {
    protected Map<String, String> applicationsAuthCookies = null;
    protected String userLogin;
    protected String userPassword;
    protected String userName;

    public ApplicationsAuth(String login, String password)
    {
        this.userLogin = login;
        this.userPassword = password;
    }

    @Override
    protected Void doInBackground(Void... params) {

        Document testPage = null;

        Connection.Response authStartResponse = null;
        Connection.Response authFinalResponse = null;


        try {
            authStartResponse = Jsoup.connect("http://support.tltsu.ru/")
                    .method(Connection.Method.GET)
                    .timeout(3500)
                    .execute();

            try {
                authFinalResponse = Jsoup.connect("http://support.tltsu.ru/login.php")
                        .timeout(3500)
                        .data("login", userLogin)
                        .data("password", userPassword)
                        .method(Connection.Method.POST)
                        .cookies(authStartResponse.cookies())
                        .execute();
                applicationsAuthCookies = authStartResponse.cookies();
                testPage = authFinalResponse.parse();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (applicationsAuthCookies != null) {
                    testPage = Jsoup.connect("http://support.tltsu.ru/")
                            .cookies(applicationsAuthCookies)
                            .timeout(3000)
                            .get();
                    userName = testPage.select("div#container > div#header > p").text();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
            super.onPostExecute(result);

    }
}

Authorization just never happening. Any suggestions? (userName using for accept or not fact of success auth, but even if you will try to see what in testPage nothing good inside it - just usal mainpage without any hints of auth)

SO FINALY! Dont know how it works but now all ok.

   class ApplicationsAuth extends AsyncTask<Void, Void, Void> {
    protected Map<String, String> applicationsAuthCookies = null;
    protected String userLogin;
    protected String userPassword;
    protected String userName;

    public ApplicationsAuth(String login, String password)
    {
        this.userLogin = login;
        this.userPassword = password;
    }

    @Override
    protected Void doInBackground(Void... params) {

        Document testPage = null;

        Connection.Response authStartResponse = null;
        Connection.Response authFinalResponse = null;


        try {
            authStartResponse = Jsoup.connect("https://support.tltsu.ru/")
                    .method(Connection.Method.GET)
                    .timeout(3500)
                    .execute();

            try {
                authFinalResponse = Jsoup.connect("https://support.tltsu.ru/login.php")
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")

                        .data("login", userLogin)
                        .data("password", userPassword)
                        .cookies(authStartResponse.cookies())
                        .method(Connection.Method.POST)
                        .timeout(3500)
                        .execute();
                applicationsAuthCookies = authStartResponse.cookies();
                testPage = authFinalResponse.parse();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (applicationsAuthCookies != null) {
                    testPage = Jsoup.connect("https://support.tltsu.ru/")
                            .cookies(applicationsAuthCookies)
                            .timeout(3000)
                            .get();
                    userName = testPage.select("div#container > div#header > p").text();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
            super.onPostExecute(result);

    }
}

Here code which now for me workable, if anyone needs it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM