- // Only add the cookies if they are not null.
- if (cookiesString != null) {
- // Add the cookies to the header property.
- httpUrlConnection.setRequestProperty("Cookie", cookiesString);
- }
- }
-
- // The actual network request is in a `try` bracket so that `disconnect()` is run in the `finally` section even if an error is encountered in the main block.
- try {
- // Get the content length header, which causes the connection to the server to be made.
- String contentLengthString = httpUrlConnection.getHeaderField("Content-Length");
-
- // Define the file size long.
- long fileSize;
-
- // Make sure the content length isn't null.
- if (contentLengthString != null) { // The content length isn't null.
- // Convert the content length to an long.
- fileSize = Long.parseLong(contentLengthString);
- } else { // The content length is null.
- // Set the file size to be `-1`.
- fileSize = -1;
- }
+ // Open a connection to the URL. No data is actually sent at this point.
+ HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);