התקן של HTTP כולל קביעה של שם משתמש וסיסמא ישירות בURL, למשל:
http://user:password@site.com/file.txt
מסיבה לא ברורה, ג’אווה לא מסתדר עם URLים כאלו ונראה שהוא לא מעביר את שם המשתמש והסיסמא לאתר.
הנה פתרון לעניין (עקום אך גנרי) :
Authenticator.setDefault(new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
URL url = getRequestingURL();
String userInfo = url.getUserInfo();
String user;
String password;
int i = userInfo.indexOf(‘:’);
if (i == -1)
{
user = userInfo.substring(0);
password = “”;
}
else
{
user = userInfo.substring(0, i);
password = userInfo.substring(i+1);
}
PasswordAuthentication ps = new PasswordAuthentication(user, password.toCharArray());
return ps;
}
});
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
URL url = getRequestingURL();
String userInfo = url.getUserInfo();
String user;
String password;
int i = userInfo.indexOf(‘:’);
if (i == -1)
{
user = userInfo.substring(0);
password = “”;
}
else
{
user = userInfo.substring(0, i);
password = userInfo.substring(i+1);
}
PasswordAuthentication ps = new PasswordAuthentication(user, password.toCharArray());
return ps;
}
});
עוגיה למי שסביר למה המימוש הסטנדרטי של ג’אווה לא תומך בדבר הטריויאלי הזה.
רסיסים (RSS)
December 2nd, 2009 ב10:47
לפי מה שאני יודע, השימוש ב-username:password כחלק מה-URL הוא לא בתקן של HTTP, למרות שבחלק מהמקרים זה עובד. אני לא יודע אם זה עובד בגלל שהדפדפנים דואגים לשים את שם המשתמש והסיסמה ב-header או שהשרת מקבל את ה-URL מהצורה הזאת.
December 2nd, 2009 ב13:56
http://www.ietf.org/rfc/rfc1738.txt
While the syntax for the rest of the URL may vary depending on the
particular scheme selected, URL schemes that involve the direct use
of an IP-based protocol to a specified host on the Internet use a
common syntax for the scheme-specific data:
user:password@host:port/url-path
December 2nd, 2009 ב14:42
באותו מסמך, בסעיף 3.3 שמתייחס ספציפית ל-HTTP כתוב:
3.3. HTTP
…..
An HTTP URL takes the form:
http://:/?
where and are as described in Section 3.1. If :
is omitted, the port defaults to 80. No user name or password is
allowed.
December 2nd, 2009 ב15:51
כנראה הדפדפנים דואגים להכניס את המידע בHEADER של הבקשה (בכל מקרה השרת לא מקבל את הURL עצמו).

נראה לי שמגיעה לך עוגיה.
קבל: