Authorization in Java using HttpClient

Question:

My task is to take data from one site (our corporate, not from the Internet, but I think it doesn't matter), I don't have access to the database and they said they won't give it, if you want to parse from the page. In general, this is the first time in this topic, before I parsed only with xml or a whole html page completely))) And now you first need to go through authorization, then enter the id of the person you need into the search engine and pull out the result from there. For two days I've been digging how I can implement it. I found several possible options. Using httpclient, but it gives me a page for entering passwords, it does not go further. That is, I enter the page after entering the passwords and username, but again they are directed to a page with a password request. Help me please.

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpBasicAuth {

public static void main(String[] args) {
    try {
        DefaultHttpClient Client = new DefaultHttpClient();
        Client.getCredentialsProvider().setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("user1", "123456789"));

        HttpGet httpGet = new HttpGet("http://10.10.151.90/default.aspx");
        HttpResponse response = Client.execute(httpGet);

        System.out.println("response = " + response);

        BufferedReader breader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuilder responseString = new StringBuilder();
        String line = "";
        while ((line = breader.readLine()) != null) {
            System.out.println(line);
            responseString.append(line);
        }

        breader.close();
       String responseStr = responseString.toString();
        System.out.println("responseStr = " + responseStr);

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

Answer:

Without knowledge of the internal structure of the site being automated, it will be quite difficult to achieve the desired result using HttpClient – only by trial and error to select the desired type of autorazition.

In your case, it is more convenient to use browser automation tools such as Selenium. I would recommend Selenide: https://ru.selenide.org/

Your code might look like:

  open("/login");
  $("#login").setValue("user");
  $("#password").setValue("password"); 
  $("#submit").click();
  $(".message").shouldHave(text("Привет"));
Scroll to Top