http Authorization

Discussion in 'Java' started by BADBOY, Jul 14, 2004.

  1. BADBOY

    BADBOY Guest

    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));}
    }
    BADBOY, Jul 14, 2004
    #1
    1. Advertising

  2. BADBOY

    Daniel Hagen Guest

    BADBOY wrote:

    > 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
    Daniel Hagen, Jul 14, 2004
    #2
    1. Advertising

  3. BADBOY

    Roedy Green Guest

    Roedy Green, Jul 14, 2004
    #3
  4. Stephen Ostermiller, Jul 15, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Wilson

    urllib2 http authorization question

    Matthew Wilson, Dec 14, 2003, in forum: Python
    Replies:
    1
    Views:
    1,352
    Troels Therkelsen
    Dec 14, 2003
  2. Christopher J. Bottaro

    HTTP GET request with basic authorization?

    Christopher J. Bottaro, Jan 2, 2005, in forum: Python
    Replies:
    1
    Views:
    556
    Fuzzyman
    Jan 4, 2005
  3. Jonas Galvez
    Replies:
    0
    Views:
    575
    Jonas Galvez
    Jan 2, 2005
  4. John J. Lee
    Replies:
    1
    Views:
    857
    John Reese
    Jan 4, 2005
  5. SeanRW
    Replies:
    1
    Views:
    350
    Dominick Baier [DevelopMentor]
    May 25, 2006
Loading...

Share This Page