http Authorization

B

BADBOY

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
ass,"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));}
}
 
D

Daniel Hagen

BADBOY said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top