Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpURLConnection request being hit twice to the server for downloading file

Following is my android code for downloading file from sever.

private String executeMultipart_download(String uri, String filepath)
            throws SocketTimeoutException, IOException {
        int count;
        System.setProperty("http.keepAlive", "false");
        // uri="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTzoeDGx78aM1InBnPLNb1209jyc2Ck0cRG9x113SalI9FsPiMXyrts4fdU";
        URL url = new URL(uri);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        int lenghtOfFile = connection.getContentLength();
        Log.d("File Download", "Lenght of file: " + lenghtOfFile);

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(filepath);
        byte data[] = new byte[1024];
        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress("" + (int) ((total * 100) / lenghtOfFile));
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        input.close();
        httpStatus = connection.getResponseCode();
        String statusMessage = connection.getResponseMessage();
        connection.disconnect();
        return statusMessage;
    }

I have debugged this code. This function is called only once even if it hits server twice. Is their any error in this code.

Thanks

like image 939
Rahul Giradkar Avatar asked Jul 26 '16 06:07

Rahul Giradkar


People also ask

Do I need to disconnect HttpURLConnection?

Yes you need to close the inputstream first and close httpconnection next. As per javadoc.

What is the difference between URLConnection and HttpURLConnection?

URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.

Which method of HttpURLConnection class is used to retrieve the response status from server?

Call setRequestProperty() method on HttpURLConnection instance to set request header values, such as “User-Agent” and “Accept-Language” etc. We can call getResponseCode() to get the response HTTP code.

What is HttpURLConnection in Java?

public abstract class HttpURLConnection extends URLConnection. A URLConnection with support for HTTP-specific features. See the spec for details. Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances.


1 Answers

Your error lies in this line:

url.openStream()

If we go to grepcode to sources of this function, then we will see:

public final InputStream openStream() throws java.io.IOException {
    return openConnection().getInputStream();
}

But you already opened connection, so you opening connection twice.

As solution you need to replace url.openStream() with connection.getInputStream()

Thus your snipped will looks like:

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.connect();
    int lenghtOfFile = connection.getContentLength();
    Log.d("File Download", "Lenght of file: " + lenghtOfFile);

    InputStream input = new BufferedInputStream(connection.getInputStream());
like image 106
Michael Spitsin Avatar answered Sep 28 '22 14:09

Michael Spitsin