javajavafxhttpsurlconnection

make an HttpsURLConnection request with parameters by method post


process an https page sending its parameters

Java8u201 using HttpsURLConnection

String httpsURL = "https://www.wmtechnology.org/Consultar-RUC/";
URL myUrl = null;

String[][] parameter = { { "modo", "1" }, { "btnBuscar", "Buscar" }, { "nruc", "10460332759" } };
System.out.println(parameter.toString());

try {
    myUrl = new URL(httpsURL);
    HttpsURLConnection conn = (HttpsURLConnection) myUrl.openConnection();


    conn.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(parameter.toString());
    wr.flush();
    wr.close();

    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String inputLine;
    while ((inputLine = br.readLine()) != null) {
        System.out.println(inputLine);
    }
    br.close();
} catch (IOException e) {
    e.printStackTrace();
}

returns the page but without data


Solution

  • You are wrong on the line

    wr.writeBytes(parameter.toString());
    

    because parameter.toString() returns string like [[Ljava.lang.String;@1f554b06 instead of expected param1=value1&param2=value2 etc.

    So correct this part to

        String parameterString = Arrays.stream(parameter)
                .map(pair -> pair[0] + "=" + pair[1])
                .collect(Collectors.joining("&"));
        wr.writeBytes(parameter.toString());