Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to connect to an url password protected?

Status
Not open for further replies.

taxista

Programmer
Feb 13, 2002
3
ES
Hi, I am doing a java program to conect to a web page that is password protected. I mean, the server asks me for a user and password to log to acess to that file. I am using the url.openconnection()method, and the url is in the shape
but I annot enter the page, though when I use that in my web browser I can do it.

What am I missing?

Thanks
 
Try doing a setAllowUserInteractionn(true) on the URL
connection.
This should allow a password dialog to pop up.
There also is a way to put the password in the header
of the request. It typically is base64 encoded but
without looking at a browser->server stream I can't remember
the syntax.

Also try a setRequestProperty(String key,String value)
on the URLConnection.
 
Try doing a setAllowUserInteractionn(true) on the URL
connection.
This should allow a password dialog to pop up.
There also is a way to put the password in the header
of the request. It typically is base64 encoded but
without looking at a browser->server stream I can't remember
the syntax.

Also try a setRequestProperty(String key,String value)
on the URLConnection.
Either of these methods I believe you'll need to change
the URLConnection parameters from
to
 
Thanks but... the first solution setAllowUserInteractionn(true), doesn't seen to work, at least, nothing different happens.(nor a pop up window...)
And the other one...the setrequest...maybe I do not know enough of this yet, cause I do not know what key or value can be...

This is part of the code I have:

static String host=" static String protocolo="http";
static int puerto = 85;
static String fichero = "/";
static DataInputStream stream;
static URLConnection urlcon = null;
static URL url = null;
static String user = "user";
static String password = "password";

/** Creates new Main */
public static void main(String[] args) {

conectar();// the function that does almost it all

}// Main()


static private void conectar()
{

// Creo la URL
try{
Authenticator.setDefault(new Validador ());

url = new URL (protocolo,host,puerto,fichero);

}
catch (MalformedURLException e) {
System.out.println("URL mal construida \n");
}
catch (IOException excepcion){
System.out.println("Error IO al crear la URLConnection " + excepcion.getMessage());
}


try {

URLConnection uc = url.openConnection();
uc.setAllowUserInteraction(true);

stream =new DataInputStream(uc.getInputStream());
stream.close();


}catch (IOException x) {
System.out.println("IOException al conectar: " + x.getMessage());
}
}
}// prueba


And the Validador class is


class Validador extends Authenticator{

String user="user";
String password="password";

Validador () {

super();

}

public PasswordAuthentication getPasswordAuthenticator ()
{
return (new PasswordAuthentication (user, password.toCharArray()));
}
}//inner class



Any help? any sugestion?

The problem is to access to a web server where that page is password protected
 
Your problem is that you need to read from the inputstream
after opening the connection.
stream =new DataInputStream(uc.getInputStream());
byte b[]=new byte[2000];
while((int i=stream.read(b,0,2000))!=-1)
System.out.print(new String(b,0,i);
stream.close();



The first solution won't help outside a browser.
For the second the key value pairs would be
("password","personsPassword") and ("user","username")

 
Hummm... well, trying not to do a long message I didn't write a sentence after this:
stream =new DataInputStream(uc.getInputStream());

that calls a procedure that reads the DataInputStream
:)

But as you say, this won't hellp outside a browser, and I am not using a browser.
About the second solution you gave me... I write just under the URLConnection creation
URLConnection uc = url.openConnection();

the following lines:
uc.setRequestProperty("user",user);
uc.setRequestProperty("password",password);
(user and password are string defined with their correct values)
But still the 401 message (unauthorized) appears, and nothing new happens.

Maybe this is going into a mess...but... what am I doing wrong?

Thanks






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top