public static HttpURLConnection getValidConnection(URL url) {
        HttpURLConnection httpurlconnection = null;

        try {
            URLConnection urlconnection = url.openConnection();
            urlconnection.addRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 5.0;Windows NT;DigExt)"); 
            urlconnection.connect();

            if (!(urlconnection instanceof HttpURLConnection)) {
                System.out.println("Not an http connection: " + url);
                // urlconnection.disconnect();
                return null;
            }

            httpurlconnection = (HttpURLConnection) urlconnection;
            // httpurlconnection.setFollowRedirects(true);

            int responsecode = httpurlconnection.getResponseCode();

            switch (responsecode) {
                // here valid codes!
                case HttpURLConnection.HTTP_OK:
                case HttpURLConnection.HTTP_MOVED_PERM:
                case HttpURLConnection.HTTP_MOVED_TEMP:
		    // System.out.println("Code: " + responsecode);
                    break;
                default:
                    System.out.println("Invalid response code: " + responsecode + " " + url);
                    httpurlconnection.disconnect();
                    return null;
            }
        } catch (IOException ioexception) {
            System.out.println("unable to connect: " + ioexception);
            if (httpurlconnection != null) {
                httpurlconnection.disconnect();
            }
            return null;
        }

        return httpurlconnection;
    }