Download the xml.gz file using HttpsURLConnection

I am trying to download the xml.gz file from a remote server using HttpsURLConnection in java, but I get an empty answer. Here is an example of my code:

URL server = new URL("https://www.myurl.com/path/sample_file.xml.gz");
HttpsURLConnection connection = (HttpsURLConnection)server.openConnection();
connection.connect();

When I try to get an InputStream from a connection, it is empty. (If I try connection.getInputStream (). Read () I get -1) The file I expect is approximately 50 MB.

To check my sanity, I also tried to enter the same url in my browser and it returned the file I needed. Am I missing something? Should I set some kind of parameter in the connection? Any help / direction is greatly appreciated.

+3
source share
3 answers

- ? SSL ? , (, , , ), .

curl wget, URL-?

, InputStream / InputStream.read() == -1, , , , .

: . , / . , , , , Java . . .

+2
  • response code 200
  • , connection.contentType, ,
  • Content-Handler MIM MIM, .

, 3xx,

  • 'connection.setFollowRedirects(true)'

.

+2

, , URL- . , connection.setFollowRedirects(true), URL :

if (connection.getResponseCode() == 302 && connection.getHeaderField("location") != null){
            URL server2 = new URL(connection.getHeaderField("location"));
            HttpURLConnection connection2 = (HttpURLConnection)server2.openConnection();
            connection2.connect();
            InputStream in = connection2.getInputStream();
}

After that I managed to get the file from the input stream. Thanks for your help guys!

+1
source

Source: https://habr.com/ru/post/1698757/


All Articles