Hi,
I am trying to get an application to post to a servlet, but keep getting
a http 401 code(invalid username or password).
Am almost certain ive got the server, username and password set up right.
Can anyone see anything wrong with code :)
private void doLogin(String name,String pass){
try{
URL url = new URL("http://127.0.0.1/servlet/jl.CheckLogin");
String
request="name="+URLEncoder.encode(name,"UTF-8")+"&pass="+URLEncoder.encode(p
a.s,"UTF-8")+"\r\n";
URLConnection c = url.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
c.setRequestProperty("Authorization:","Basic
"+authName+":"+authPass);
c.setRequestProperty("Content-type","application/x-www-form-urlencoded");
c.setRequestProperty("Content-Length",""+request.length());
DataOutputStream out = new
DataOutputStream(c.getOutputStream());
out.writeBytes(request);
out.flush();
out.close();
// DataInputStream in =new DataInputStream(c.getInputStream());
// Handle data from servlet through in.
}catch(MalformedURLException murle){
statusLabel.setText(String.valueOf(murle));}
catch(IOException ioe){ statusLabel.setText(String.valueOf(ioe));}
}
Daniel Hagen - 14 Jul 2004 15:32 GMT
> c.setRequestProperty("Authorization:","Basic
> "+authName+":"+authPass);
You have to Base64-encode the "user:password"-part of the authorization
header.
Anyway, the right way to do this would be to use a java.net.Authenticator.
Regards
Daniel
Roedy Green - 14 Jul 2004 18:36 GMT
>Anyway, the right way to do this would be to use a java.net.Authenticator.
see
http://mindprod.com/jgloss/authentication.html
for sample code.

Signature
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Stephen Ostermiller - 15 Jul 2004 12:19 GMT
See the example of Base64 encoding for basic authentication to a web server here:
http://ostermiller.org/utils/Base64.html