S
S
shipovalov2015-03-19 08:05:56
Java
shipovalov, 2015-03-19 08:05:56

How to open a page in JAVA, enter a login and password, and after logging in, download a file?

You need to go to the /accounts/login/ page , enter your login and password. After logging in, download the file from the link /file/ .

<form action="/accounts/signin/" method="POST">
<p><b>Введите логин</b></p>
<p><input type="text" name="form[login]" onclick="if (this.value == 'личный № организации') this.value='';" onblur="if (this.value == '') this.value='Поиск по сайту';" value="личный № организации" style="background:url('/images/men.png') no-repeat scroll 7px 3px transparent;padding-left:30px;"></p>
<p><b>Введите пароль</b></p>
<p><input type="password" name="form[password]"  onclick="if (this.value == 'личный № организации') this.value='';" onblur="if (this.value == '') this.value='Поиск по сайту';" value="личный № организации" style="background:url('/images/key.png') no-repeat scroll 7px 3px transparent;padding-left:30px;"></p>
<p>&nbsp;</p>
<input type="submit" value="Войти в кабинет">

Tried like this
Connection.Response loginForm = Jsoup.connect("http://site/accounts/login/")
            .method(Connection.Method.GET)
            .execute();

    Document doct = Jsoup.connect("http://site/accounts/login/")
            .data("cookieexists", "false")
            .data("form[login]", "LOGIN")
            .data("form[password]", "PASSWORD")
            .cookies(loginForm.cookies())
            .post();

in doc you get the HTML code of the page where you need to enter the login - password, and not the page after entering.
I tried to change the URL to the one in the form /accounts/signin/ - the same thing.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Santer_ua, 2015-03-19
@Santer_ua

I think Selenium WebDriver will suit you, it is used for automated testing.

E
Eugene, 2015-03-19
@zolt85

Jsoup is a parser. He won't help you. Because submitting form data is a request, then you need to manipulate the request. This can be done using Apache HttpClient , for example...

E
exenza, 2015-03-19
@exenza

Try like this:

Response res = Jsoup
    .connect("/url/to/login")
    .data("input[login]", "admin")
    .data("input[pass]", "QWERTY1234")
    .method(Method.POST)
    .execute();
Document doc = res.parse();

Document doc2 = Jsoup
    .connect("/url/to/file")
    .cookies(res.cookies())
    .get();

Or a high-level jWebUnit library, it's quite simple

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question