0

Good evening together. My problem is that if I open an url and fetch the content it ALWAYS fails on the second loop run (yes the urls are all correct). SDK: 11.0.1

public String getUrlContent(String url) {
        System.out.println("URL: " + url);
        for (int i = 0; i < 100; i++) {
            try (Scanner scanner = new Scanner(new URL(url).openStream(),
                    StandardCharsets.UTF_8.toString())) {
                scanner.useDelimiter("\\A");
                String returnString = scanner.hasNext() ? scanner.next() : "";
                scanner.close();
                return returnString;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("###############################################");
                System.out.println("#Connection failed..#");
                System.out.println("#Trying again...                           #");
                System.out.println(e);
                System.out.println("###############################################");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
        System.out.println("Connection failed to often. Program closed.");
        System.exit(0);
        return null;
    }

What did I try? when i call the function in a test file, the error does not occur. however, the function contains the same code. do you have any ideas? As I said, it always aborts at the second url. the code around the function should have no impact, because only the results are stored in an array, which I can get from the object.

Error:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:128)
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:308)
    at java.base/sun.security.ssl.Alert$AlertConsumer.consume(Alert.java:279)
    at java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:181)
    at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:164)
    at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1152)
    at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1063)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:402)
    at java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:567)
    at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1581)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
    at java.base/java.net.URL.openStream(URL.java:1117)
    at helper.webHelper.getUrlContent(webHelper.java:42)
    at urlParser.parser.checkLastUpdate(parser.java:41)
    at main.main(main.java:29)
Cyberpunk7711
  • 127
  • 1
  • 10
  • It can't fail at the second loop run unless it also failed at the first. Parsing data in a loop has nothing to do with it. You got an SSL handshake exception when opening the URL, not when parsing data. – user207421 Nov 03 '20 at 01:35
  • my parser has the following procedure: first i get all urls. from one page(no error). then the urls are processed in a loop and saved in the database. and as i said, in the first run the url is parsed without problems and all data is saved. always from the second run on i get the error. if i open the urls (which i of course output) directly, it is possible without problems. but as i said, always from the second run on... is it possible that i have to close something because of the url which i did not do? Translated with www.DeepL.com/Translator (free version) – Cyberpunk7711 Nov 03 '20 at 01:56
  • Does the server support TLS1.3? If so, possibly dupe https://stackoverflow.com/questions/53913758/java-11-https-connection-fails-with-ssl-handshakeexception-while-using-jsoup -- try running with sysprop `javax.net.debug=ssl:handshake` and look at the second ClientHello. @MarquisofLorne: the exception _does_ occur in URL.openStream(), see the stacktrace. – dave_thompson_085 Nov 03 '20 at 03:37

0 Answers0