Search code examples
javaandroidhttpdownloadhttp-authentication

Download a file through http-auth


I can't download file with basic http-auth. I have auth string for authorization, but i get exception: FileNotFound. Code:

URL url = new URL(params[0]);
                URLConnection  conection =  url.openConnection();

                conection.setRequestProperty("Authorization", authString);
                conection.setRequestMethod("GET");
                conection.setAllowUserInteraction(false);
                conection.setDoInput(true);
                conection.setDoOutput(true);
                conection.connect();



                int lenghtOfFile = conection.getContentLength();


                // Exception on line below
                BufferedInputStream  input = new BufferedInputStream(conection.getInputStream()); 

                // Output stream
                OutputStream output = new FileOutputStream(filePath);

                byte data[] = new byte[1024];
...

lenghtOfFile isn't 0. Besides, I tried using HtppClient for this task, but get exception too.


Solution

  • As I have commented, you should remove the line setDoOutput(true) because this is GET request.