Java Tip: Basic Authentication with HttpURLConnection Java Sugerencia: la autenticación básica con HttpURLConnection
Java provides a super simple, yet hidden from plain view, way to do basic authentication of HttpURLConnection / URLConnection. Java proporciona una super sencillo, pero oculto a la vista, la forma de hacerlo autenticación básica de HttpURLConnection / URLConnection.
Before making a connection add the following lines of code: Antes de realizar una conexión añadir las siguientes líneas de código:
final String login ="..."; final String password ="..."; Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication (login, password.toCharArray()); } }); final String login ="..."; final String contraseña ="..."; Authenticator.setDefault (nuevo autenticador () (protegidas PasswordAuthentication getPasswordAuthentication () (retorno nuevo PasswordAuthentication (login, password.toCharArray ());)) ); This sets your default Authenticator which is called whenever authentication is required for any URLConnection. Esto establece su defecto autenticador que se llama cuando se requiere autenticación para cualquier URLConnection. Problem solved. Problema resuelto.
Note: final is required for the inner class to access the variable. Nota: El final es necesaria para que el interior de la clase para acceder a la variable.
Authenticator is available since JDK 1.2 and yet very few information is there about it on the web. Autenticador está disponible desde el JDK 1,2 y, sin embargo, muy pocos hay información al respecto en la web. Almost everyone recommend using a class from Sun package ( sun.misc.BASE64Encoder() ) and do the encoding manually. Casi todo el mundo recomiendan el uso de una clase de paquete de Sun (sun.misc.BASE64Encoder ()) y hacer la codificación manual. None of this is required in the simple solution above. Nada de esto se requiere en la solución sencilla.
The Java URLConnection API should have a setAuthenticator(Authenticator) method for making it easier to use this class in multi-threaded context where authentication is required. El Java API URLConnection debe tener un setAuthenticator (autenticador) el método para hacer más fácil a utilizar esta clase en varios subprocesos contexto en el que se requiere autenticación.
Filed under Filed under Headline News Headline News , How To Cómo , Java Software El software de Java , Programming Programación , Tech Note Nota técnica , Web , Web Services Web Services | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | este artículo |
Email this Article Enviar artículo
You may also like to read También puede leer |




