Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Networking Question

Status
Not open for further replies.

cvf

Programmer
Oct 14, 2001
5
MY
Hello all,
How do I access a 'protected' url ie. a url protected with a username/password combination. Usually it is of the form Whnever I attempt this form of URL, Java gives me an exception.

Thanks,
VFC
 
Hi!

I hope this snippet will help you:

URL urlTxt=new URL("URLConnection urlConn=urlTxt.openConnection();
urlConn.setRequestProperty ("Authorization", "Basic "+
encodeBASE64("login_name:password"));
InputStream in=urlConn.getInputStream();

Good luck.

Otto
 
Thanks Otto but how do I instantiate encodeBase64, which class do I import?

Thanks,
VFC
 
Sorry. Here is a simple encoder:

************** cut here *********************
public class Base64 {
// the Base64 printable encoding characters
static char[] ENC = {
'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','+','/'
};

// function encode takes the "username:password" string and
// converts it into the printable encoding format

public static String encode(String string) {

int i, j;
byte[] byte_array = new byte[3];
StringBuffer buf_coded = new StringBuffer();

// get length of input string
int nbytes = string.length();

for (i = 0; i < nbytes; i+= 3) {

// check to make sure we don't run off the end of input string
if (i + 3 < nbytes)
j = i + 3;
else
j = nbytes;

string.getBytes(i, j, byte_array, 0); // get bytes i..j

if (j - i == 1) {
// missing last two bytes
byte_array[1] = 0;
byte_array[2] = 0;
}

if (j - i == 2) {
// missing last byte
byte_array[2] = 0;
}

// convert the three bytes into four Base64 characters
// and append to the buf_coded string buffer
buf_coded.append(ENC[byte_array[0] >> 2]);
buf_coded.append(ENC[((byte_array[0] << 4) & 060) | ((byte_array[1] >> 4) & 017)]);
buf_coded.append(ENC[((byte_array[1] << 2) & 074) | ((byte_array[2] >> 6) & 03)]);
buf_coded.append(ENC[byte_array[2] & 077]);

} // end for loop

// If nbytes was not a multiple of 3, then we have encoded too
// many characters. Adjust appropriately.

int buf_length = buf_coded.length();

if (i == nbytes+1) {
/* There were only 2 bytes in that last group */
buf_coded.setCharAt(buf_length - 1, '=');
} else if (i == nbytes+2) {
/* There was only 1 byte in that last group */
buf_coded.setCharAt(buf_length - 1, '=');
buf_coded.setCharAt(buf_length - 2, '=');
}

// return the Base64 encoded string
return buf_coded.toString();

} // end String encode (String)
}
******************** cut here ***********************

Otto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top