Krishna
Krishna

Reputation: 1

Continous loading during HttpURLConnection

I have a URL which is giving a QR Image response from a web URL. When that URL is restricted in Application running server this block code is continuously loading not returning any value. I am not sure about which line of code. But My guess is When getting response code it may not return the value. But I have checked with some other URL by restricting in application running server in Local, but exception caught properly. In production above issue occurred. Please any one help me to fix this issue.

try {
    URL url = new URL(constructedUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("User-Agent", "Mozilla/5.0");
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        inputStream = connection.getInputStream();

        // Load the image
        BufferedImage originalImage = ImageIO.read(inputStream);

        //Reading a image and written local server

    } else {
        exceptionOccurred = true;
        log.log(Level.SEVERE, "Unable to establish the connection while generating a QR code Responce Code :"+connection.getResponseCode());
    }
} catch (MalformedURLException e) {
    exceptionOccurred = true;
    log.log(Level.SEVERE, "MalformedURLException Occurred while generating a QR code ");
    e.printStackTrace();
} catch (IOException e) {
    exceptionOccurred = true;
    log.log(Level.SEVERE, "IOException Occurred while generating a QR code");
    e.printStackTrace();
} catch (Exception e) {
    exceptionOccurred = true;
    log.log(Level.SEVERE, "Exception Occurred while generating a QR code");
    e.printStackTrace();
} finally {
    if (inputStream != null) {
        try {
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Reason for continuous loading. How to handle the code? Need to set timeout?

Upvotes: 0

Views: 35

Answers (0)

Related Questions