Forum Stats

  • 3,769,823 Users
  • 2,253,026 Discussions
  • 7,875,217 Comments

Discussions

Http Header and Java Client

843807
843807 Member Posts: 46,582
edited Oct 16, 2002 8:45AM in Java Desktop Applications
I want to connect a WebSite with ASP technic. It need username and password to login. I know username and password.
But i want to connect it in a Java Client. First i login, then i get another page file.
What can i do in a main method?
Had better give me an example.
Thank your help!

Comments

  • 843807
    843807 Member Posts: 46,582
    I'm trying to understand what you're asking. I'm guessing it's something like ...

    1- You want to write a java client to interact with an ASP-implemented web site.

    2- In the login interaction with that web site, you need to provide username and password, etc.

    3- You're stuck because in some cases HTTP interactions require multiple steps.

    There's a book I have that's pretty helpful in this regard, titled "Programming Spiders, Bots, and Aggregators in Java " (http://www.amazon.com/exec/obidos/ASIN/0782140408/thereikipageA/).

    - David
  • 843807
    843807 Member Posts: 46,582
    I havn't tested this code so there may be some typing errors. All you have to do is open a connection to the site, set a headerfield with the username and password (notice the colon inbetween the username and password) and then open an input stream.
    URL url = new URL("http://www.company.com");
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("Authorization", "Basic " + Base64.encode("username:password"));
    InputStream inputStream = connection.getInputStream();
    ------> Copy and paste this as a class-file.
    import java.io.*;
    
    public class Base64
    {
       private static final char [] BASE64CHARACTERS = 
       {
          'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
          'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
          'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
          'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
       };
    
       public static String encode(byte [] aData)
       {
          StringBuffer output = new StringBuffer();
    
          for (int i = 0; i < aData.length;) 
          {
             int value = 0;
             int length = 0;
    
             for (int j = 2; j >= 0 && i < aData.length; j--, length++)
             {
                value += ((int)aData[i++] & 255) << (8 * j);
             }
    
             if (length >= 1) output.append(BASE64CHARACTERS[(value >> 18) & 63]);
             if (length >= 1) output.append(BASE64CHARACTERS[(value >> 12) & 63]);
             if (length >= 2) output.append(BASE64CHARACTERS[(value >>  6) & 63]);
             if (length >= 3) output.append(BASE64CHARACTERS[(value      ) & 63]);
          }
    
          output.append("====".substring(0, output.length() % 4));
    
          return output.toString();
       }
    }
This discussion has been closed.