Why conn.getResponseCode() throws an SSLHandShakeException "Received fatal alert: handshake_failure"?

31 Views Asked by At

I'm trying to get a JSON from moodle. My request is working from a browser bar and also with curl, but not from java.

Here's a small piece of code that shows the issue.

public static void main(String[] args) throws IOException, JSONException {
    String sURL = "https://proves.aprenonline.org/webservice/rest/server.php?"
            + "wstoken=53a56e582ce12345678907e156d578a585&"
            + "moodlewsrestformat=json&"
            + "wsfunction=core_course_get_courses";
    
    URL url = new URL(sURL);
    System.setProperty("https.protocols", "TLSv1.2"); 
    HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(10000);
    conn.setRequestMethod("GET");
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    int responseCode = 0;
    try {
         responseCode = conn.getResponseCode();
    }
    catch(Exception x) {
        System.out.println(x.getMessage());
    }

Can any of you guys tell me why this code throws a SSLHandShakeException saying: "Received fatal alert: handshake_failure". The request is right, I insist connecting via browser or command line works ok.

Same problem with all related methods: getInputStream(), openStream(),...

Any idea?

Help will be infinitely appreciated!

Carles

0

There are 0 best solutions below