Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2004

Tip: Looking for answers? Try searching our database.

http Authorization

Thread view: 
BADBOY - 14 Jul 2004 11:43 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.