|
Java™ by example!
|
|
|
How do I get a page when HTTP authentification is required?
In your browser, you can type: http://username:password@host.com/securestuff to automatically authenticate if this page is protected. You can't do this with the URL class, it will come back with an UnknownHostException. But there is a way: create a URLConnection and set the authorization request property (for more information on request properties, check out the link in related links). This example shows you how to retrieve the data on a protected page providing you have the username and password. It makes use of the sun.misc package to do the required Base64 encoding of the concatenated string "username:password". You may want to replace the use of the "sun.misc" package by your own implementation of Base64 (or another provider) as the availability of sun. classes is not guaranteed in future versions of the JDK. Main.java:
Further Information
Author of answer: Joris Van den Bogaert
Comments
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|