0

I am using HttpURLConnection package to download a large file. I am able to do so but I am bit confuse between HttpURLConnection.connect vs URL.openurlconnection methods.

The URL.openurlconnection returns HttpURLConnection. So can I reuse this object ? If the network disconnects and then connects back, I would like to continue downloading the file. Can I directly call connect() or do I have to reconfigure the connection ?

Link to the documentation: HttpURLConnection: http://developer.android.com/reference/java/net/HttpURLConnection.html URL: http://developer.android.com/reference/java/net/URL.html

Thanks

Arjun Patel
  • 345
  • 6
  • 22

1 Answers1

2

Difference between openurlconnection vs connect?

They are equivalent.

The URL.openurlconnection returns HttpURLConnection. So can I reuse this object ? If the network disconnects and then connects back, I would like to continue downloading the file. Can I directly call connect() or do I have to reconfigure the connection ?

A HttpURLConnection instance is used to make a single request. It cannot be reused. If you want to continue downloading a file following failure, you need to create a new connection.

If you want to avoid transferring again the part of the file that you fetched previously, you could use a "Range Retrieval Request". But be aware that server-side support for range retrieval is optional, so the client-side needs to pay close attention to the response headers to figure out whether it is getting the entire file or just the requested range.

Related Question:

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216