Unable to get Etag for larger online txt file uploaded to dropbox

193 Views Asked by At

I need to get the etag value of a txt file from its URL, the txt file is uploaded on dropbox.

The etag is a part of the http header of the txt url.

If the file is really small, say 5-10 characters, the code shows the etag value np.

But if the file is 4000 characters or more it returns a null for the etag value. Help needed here, I do not know why this happens.

Here's my code.

public void getDatabaseEtag(){
        try {


            URL url = new URL("https://dl.dropboxusercontent.com/<EDITED>.txt");
            HttpURLConnection.setFollowRedirects(true);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(false);
            con.setReadTimeout(20000);
            con.setRequestProperty("Connection", "keep-alive");

            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
            ((HttpURLConnection) con).setRequestMethod("HEAD");


            Map<String, List<String>> map = con.getHeaderFields();

            System.out.println("Printing Response Header...\n");

            for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                System.out.println("Key : " + entry.getKey() 
                        + " ,Value : " + entry.getValue());
            }

            System.out.println("\nGet Response Header By Key ...\n");
            String etag = con.getHeaderField("ETag");

            if (etag == null) {
                System.out.println("Key 'ETag' is not found! in GetDatabaseEtag()");
            } else {
                //to string in internal storage etag
                stringToTxt(etag,"data_etag.txt");
                System.out.println("ETag - " + etag);
            }

            System.out.println("\n Done");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
0

There are 0 best solutions below