The simplest way to download a file from http resource that needauthentication

A

Andrea Francia

I need write a program which download many files from different web sites.

I found that the following code let me download a file:

URL url = new URL("http://www.example.org/file.txt");
URLConnection con = url.openConnection();

BufferedInputStream in = new BufferedInputStream(con.getInputStream());
OutputStream out = new FileOutputStream("C:\\file.txt");

int i = 0;
byte[] bytesIn = new byte[8096];
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
out.close();
in.close();

But it doesn't work if the web site need an authentication.
If I use
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top