Java Reference
In-Depth Information
d.download("https://www.httprecipes.com/1/5/secure/",
"./test.html",
"testuser",
"testpassword");
} else
{
d.download(args[0], args[1], args[2], args[3]);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
This program can be passed a URL, local filename, user id and password on the com-
mand line. If no parameters are provided, the program will default to the following URL:
https://www.httprecipes.com/1/5/secure/
The program will also default to a local file of test.html , a user id of testuser ,
and a password of testpassword .
You can also specify a URL. For example, to run this recipe with the URL of
http://www.httprecipes.com/ , a local file of index.html , a user id of user and
a password of password , you would use the following command:
AuthDownloadURL http://www.httprecipes.com ./index.html user pass-
word
The above command simply shows the abstract format to call this recipe, with the ap-
propriate parameters. For exact information on how to run this recipe refer to Appendix B, C,
or D, depending on the operating system you are using. This recipe is very similar to Recipe
4.3, except that it can download from an HTTP authenticated site as well as a regular site.
Only the new code relating to HTTP authentication will be discussed here. If you would like
to review how this recipe actually downloads a binary or text file, refer to Recipe 4.3. The
downloading code is the same in both recipes.
To access an HTTP authenticated site, the program makes use of a method called
addAuthHeader . This method adds the Authorization header to the
HttpURLConnection object. This process is shown here.
String hdr = uid + ":" + pwd;
First the addAuthHeader method builds the header. This header is very simple, it
is just the user id delimited by a colon (:) followed by the password.
Search WWH ::




Custom Search