1

I am using HttpURLConnection to download a xml file. I can see the file in my web browser, it takes over 10 seconds to load. But i can see the content eventually. But I am not able to download it through my java code. It seems like all my SetTimeOut() don't really work. Here is my code, please help:

        HttpURLConnection con = (HttpURLConnection) laURL.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

        con.setConnectTimeout(15*1000);
        con.setReadTimeout(15*1000);

        InputStream ins = con.getInputStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader in = new BufferedReader(isr);

        String inputLine;
        while ((inputLine = in.readLine()) != null)
        {
            System.out.println(inputLine);
        }
        in.close();
1
  • You don't need to set the Content-Type header in your request. This is telling the server that the content of the request body is of type text/xml. You don't have a request body. You might want Accept header. Sep 9, 2013 at 17:18

1 Answer 1

0

Hmmm...can you download it as a binary file?

Try this:

        con.setRequestProperty("content-type", "binary/data");

        try (InputStream in = con.getInputStream(); FileOutputStream out = new FileOutputStream(destinationPath))
        {
            byte[] b = new byte[1024];
            int count;

            while ((count = in.read(b)) > 0)
            {
                out.write(b, 0, count);
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.