HTTP authentication

Discussion in 'Java' started by Randy, Nov 22, 2004.

  1. Randy

    Randy Guest

    Help!

    I can't seem to get this code to work. I have found numerous sites
    that show the same code that I am using.

    I am using jdk 1.5 and trying to generate the
    "Authorization: Basic erwerwqerwerwer" line in the http get header. I
    have tried the "Authenticator.setDefault (new MyAuthenticator ());"
    route and it did not work. I have found a lot of web sites with the
    code snippets

    // Build the string to be used for Basic Authentication
    <username>:<password>
    String userPassword = "username:password";

    // Base64 encode the authentication string
    String encoding = new sun.misc.BASE64Encoder().encode
    (userPassword.getBytes());

    URL url = new URL ("http://www.server.com");
    HttpURLConnection connection =
    HttpURLConnection)url.openConnection();

    connection.setDoInput(true);
    connection.setUseCaches(false);
    connection.setRequestProperty("Authorization", "Basic " +
    encoding);
    connection.connect();

    BufferedReader in =
    new BufferedReader (new InputStreamReader
    ((InputStream)url.getContent()));

    String line;
    while ((line = in.readLine()) != null) {
    pw.println (line);
    }



    but none of this works. All I get is:

    GET / HTTP/1.1
    User-Agent: Java/1.4.2
    Host: www.server.com
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive



    Either the JDK does not work or I am doing something stupid. Help!
    Please? I am about to just say screw it and use a raw socket and write
    the get request manually.

    Thanks!
     
    Randy, Nov 22, 2004
    #1
    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. Jiong Feng
    Replies:
    0
    Views:
    838
    Jiong Feng
    Nov 19, 2003
  2. Paul Glavich [MVP - ASP.NET]
    Replies:
    0
    Views:
    449
    Paul Glavich [MVP - ASP.NET]
    Jun 20, 2004
  3. T. Regan
    Replies:
    3
    Views:
    712
    T. Regan
    Jun 21, 2004
  4. Max
    Replies:
    2
    Views:
    1,109
  5. Nacho Nachev
    Replies:
    2
    Views:
    907
Loading...

Share This Page