Use the Java built-in Authenticator instead:
--- Create a class that subclasses Authenticator. Make it inner if you want to use user-provided usernames and passwords.
import java.net.*;
class MyAuth extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password.toCharArray());
}
}
--- Then in your servlet, before you open your connection, add the following line:
Authenticator.setDefault(new MyAuth());
> -----Original Message-----
> From: Alfonso Urdaneta [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, May 26, 1999 4:53 PM
> To: [EMAIL PROTECTED]
> Subject: URLConnection.setRequestProperty - Authorization
>
>
> Howdy all,
>
> I'm trying to make a servlet act as a filter for requests
> going to a 2nd
> web site, like so.
>
> user ---> servlet ---> second site.
>
> The second site is using basic authentication. Now what I
> want to do is
> have the servlet pass a userid:password to server2 based on session
> information.
>
> I'm doing the following :
>
> URL url = new URL( hostname + uri );
> URLConnection urlc = url.openConnection();
> String password = "username:password";
> String encoded = HTTPClient.Codecs.base64Encode(password);
> String basic = "Basic " + encoded ;
> urlc.setRequestProperty( "Authorization",
> basic );
> int loop=0;
> String header_resp;
> while( ( header_resp = urlc.getHeaderField(loop)) != null )
> {
> System.out.println( loop + " : " + header_resp );
> loop++;
> }
> InputStream is = url.openStream();
> int i;
> while( (i = is.read()) != -1 ) { out.write(i); }
> is.close();
> out.close();
>
> Now comes the bizarre part. I keep seeing the 401.2 (authorization
> denied) page.
>
> However, when I dump the headers of the response, I get the following:
>
> HTTP/1.0 200 OK
> Microsoft-IIS - 4.0
> <todays date>
> text/html
> bytes
> <doc creation date>
> "some cryptic strings"
> 3504
>
> Why am I getting the 200 OK, and the 401.2 pages ? Aren't these are
> mutually exclusive, or am I on crack ?
>
> Alfonso.
>
> ______________________________________________________________
> _____________
> To unsubscribe, send email to [EMAIL PROTECTED] and
> include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources:
http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
