Can not fetch data (JSON) from url - Java

Viewed 28

I'm making a java program and I want to fetch updates (json data) from URL of my website. I used HttpURLConnection and Pass SSL Certificate. I got this error while running:

javax.net.ssl.SSLException: Received fatal alert: internal_error
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
    at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2011)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1113)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)

(HTTPS URL)
I also try HTTP URL but the response code is 308

Pass SSL Certificate code:

    public static void setupIgnoreSSLCertificate() throws NoSuchAlgorithmException, KeyManagementException {
        final TrustManager[] trustManager = new TrustManager[]{new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }};

        final SSLContext disease = SSLContext.getInstance("SSL");
        disease.init(null, trustManager, new SecureRandom());

        HttpsURLConnection.setDefaultSSLSocketFactory(disease.getSocketFactory());

        final HostnameVerifier aids = (s, sslSession) -> true;
        HttpsURLConnection.setDefaultHostnameVerifier(aids);
    }

fetch code:

        StringBuilder text = new StringBuilder();
        try {
            setupIgnoreSSLCertificate();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new RuntimeException(e);
        }
        try {
            URL url = new URL(base + method);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            int code = connection.getResponseCode();
            if(code != 200) {
                System.out.println("Error code: " + code);
            } else {
                Scanner scanner = new Scanner(url.openStream());
                while(scanner.hasNext()) {
                    text.append(scanner.nextLine());
                }
                scanner.close();
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
0 Answers
Related