-1

我正在尝试创建一个将登录到本地网站的 java 应用程序。问题是“线程“主”javax.net.ssl.SSLHandshakeException 中的异常:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径”以及登录的事实页面需要通过我可以检查的警报框登录(或不知道如何)

我建立连接的java类是:

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;
}

}

页面如下所示:

在此处输入图像描述

正如您所看到的,它只是 HTTP,所以我认为这可能是一个问题,因为我在这里什至没有证书。

帮助将不胜感激!!!提前致谢!

4

1 回答 1

0

根据 SSL 的第一部分,http: //abhinavasblog.blogspot.com/2011/07/allow-untrusted-certificate-for-https.html 如果您创建一个新类并使用提供的代码,ofc 与关闭 SSL 可信认证连接的风险由您自己承担。现在第二部分阻碍了我,因为我找不到任何 ID 也不允许我登录

于 2021-08-04T14:23:17.957 回答