'java.net.URL giving security vulnerability during code scan

I have the below method to check an incoming url is valid or not and based on this i need to redirect to error page.

/*
 * This method is used to validate the URL and return a response
 */
private static int isValidUrl(String qrUrl) {
    int code = 0;
     try {
            URL url = new URL(qrUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            code = connection.getResponseCode();  
            logger.info("Response code is {} for the url", code);
        } catch(MalformedURLException e) {
            logger.error("A MalformedURLException Occured in QR Code servlet during URL Validation", e);
        } catch (IOException e) {
            logger.error("An IOException Occured in QR Code servlet during URL Validation", e);
        } 
    return code;
    

}

The below line of code is giving medium security vulnerability(Description: This web server request could be used by an attacker to expose internal services and filesystem) during code scan. How can I fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source