我做一些簡單的HTTP認證時,出現了的Java:與HTTPBasic抓取網址驗證
java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic OGU0ZTc5ODBk(...trimmed from 76 chars...)
(...more password data...)
我想這是因爲我有一個很長的用戶名和密碼和編碼器與包裝它\n
在76個字符。有什麼方法可以解決這個問題嗎?該URL僅支持HTTP基本身份驗證。
這裏是我的代碼:
private class UserPassAuthenticator extends Authenticator {
String user;
String pass;
public UserPassAuthenticator(String user, String pass) {
this.user = user;
this.pass = pass;
}
// This method is called when a password-protected URL is accessed
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass.toCharArray());
}
}
private String fetch(StoreAccount account, String path) throws IOException {
Authenticator.setDefault(new UserPassAuthenticator(account.getCredentials().getLogin(), account.getCredentials().getPassword()));
URL url = new URL("https", account.getStoreUrl().replace("http://", ""), path);
System.out.println(url);
URLConnection urlConn = url.openConnection();
Object o = urlConn.getContent();
if (!(o instanceof String))
throw new IOException("Wrong Content-Type on " + url.toString());
// Remove the authenticator back to the default
Authenticator.setDefault(null);
return (String) o;
}
的安全通知:如果通過HTTP使用的用戶名和密碼,這兩個事情發送清晰的字符串。最好使用不同的身份驗證(表單,POST)和傳輸協議(HTTPS,H2)。 – tfb785 2017-06-01 12:50:11