-1

I'm trying to create a java app that will login to al local website. The problem is the "Exception in thread "main" javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target" AND the fact that the login page requires to login throught a alert box which i can inspect (or dont know how to)

My java class to establish a connection is:

public class EstablishConnectionClass {

//start with a alist for cookies to keep the session
private List<String> cookies;
//next the variable to establosh a conn
private HttpURLConnection conn;

private final String USER_AGENT = "Mozilla/5.0";

public static void main(String[] args) throws Exception{
    String url = "http://10.10.100.200/";
    String sms = "http://10.10.100.200/cgi-bin/php/sms-settings.php";

    EstablishConnectionClass http = new EstablishConnectionClass();

    //make sure cookies are on
    CookieHandler.setDefault(new CookieManager());

    //send a GET request sp that we know the form's data
    String page = http.GetPageContent(url);
    String postParam = http.getFormParams(page, "admin", "admin123");

    //Contruct the POST content and send a POST request
    http.sendPost(url, postParam);

    //When connection is established reply with a result
    String result = http.GetPageContent(sms);
    System.out.println(result);
}

private void sendPost(String url, String postParams) throws Exception{

    URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
    conn = (HttpURLConnection) obj.openConnection();

    //Act like a browser
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Host", "http://10.10.100.200/");
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept",
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
    for (String cookie : this.cookies) {
        conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
    }
    conn.setRequestProperty("Connection", "keep-alive");
    conn.setRequestProperty("Referer", "http://10.10.100.200/cgi-bin/php/sms-settings.php");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));

    conn.setDoOutput(true);
    conn.setDoInput(true);

    //send POST request
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(postParams);
    wr.flush();
    wr.close();

    int responseCode = conn.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + postParams);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in =
            new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    // System.out.println(response.toString());

}

private String GetPageContent(String url) throws Exception {

    URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
    conn = (HttpsURLConnection) obj.openConnection();

    // default is GET
    conn.setRequestMethod("GET");

    conn.setUseCaches(false);

    // act like a browser
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept",
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
    if (cookies != null) {
        for (String cookie : this.cookies) {
            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
        }
    }
    int responseCode = conn.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in =
            new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    // Get the response cookies
    setCookies(conn.getHeaderFields().get("Set-Cookie"));

    return response.toString();

}
public String getFormParams(String html, String username, String password)
        throws UnsupportedEncodingException {

    System.out.println("Extracting form's data...");

    Document doc = Jsoup.parse(html);

    // Google form id
    //Doubt
    Element loginform = doc.getElementById("alertbox");
    Elements inputElements = loginform.getElementsByTag("input");
    List<String> paramList = new ArrayList<String>();
    for (Element inputElement : inputElements) {
        String key = inputElement.attr("name");
        String value = inputElement.attr("value");

        //Doubt
        if (key.equals("Username"))
            value = username;
        //Doubt
        else if (key.equals("Password"))
            value = password;
        paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
    }

    // build parameters list
    StringBuilder result = new StringBuilder();
    for (String param : paramList) {
        if (result.length() == 0) {
            result.append(param);
        } else {
            result.append("&" + param);
        }
    }
    return result.toString();
}

public List<String> getCookies() {
    return cookies;
}

public void setCookies(List<String> cookies) {
    this.cookies = cookies;
}

}

The page looks like this:

enter image description here

As you can see it's HTTP only so I thought that that might be an issue as I dont even have a certificate here.

Help would be highly appreciated!!! Thanks in advance!

  • There is too much code here, and not enough error information. The exception is coming from a single connection attempt, and it looks like you don’t even know what code is making that attempt. Please post an [mcve]. – erickson Aug 04 '21 at 14:30

1 Answers1

0

As per the first part for the SSL, http://abhinavasblog.blogspot.com/2011/07/allow-untrusted-certificate-for-https.html this helps if you create a new class and use the code provided, ofc with your own risk of shutting down SSL trusted certification connection. Now the 2nd part is what hinders me, since I cant find any ID nor will it allow me to log-in