Java:使用 HTTPBasic 身份验证获取 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2054687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: fetch URL with HTTPBasic Authentication
提问by Paul Tarjan
I'm doing some simple HTTP authentication and am getting a
我正在做一些简单的 HTTP 身份验证,并得到一个
java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic OGU0ZTc5ODBk(...trimmed from 76 chars...)
(...more password data...)
which I think is due to me having a really long username and password and the encoder wraps it with a \nat 76 chars. Is there any way I can get around this? The URL only supports HTTP Basic Auth.
我认为这是因为我有一个非常长的用户名和密码,编码器用\n76 个字符包装它。有什么办法可以解决这个问题吗?该 URL 仅支持 HTTP 基本身份验证。
Here is my code:
这是我的代码:
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;
}
回答by Thilo
That seems to be a bug in Java.
Have you tried using alternative HTTP clients, such as the library from Apache?
您是否尝试过使用其他 HTTP 客户端,例如来自 Apache 的库?
Or instead of using the Authenticator, manually setting the header?
还是不使用身份验证器,而是手动设置标头?
URL url = new URL("http://www.example.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Basic OGU0ZTc5ODBkABcde....");
The token value is encodeBase64("username:password").
令牌值为 encodeBase64("username:password")。
回答by user2862891
This works for me .
这对我有用。
HttpsURLConnection con = null; con = (HttpsURLConnection) obj.openConnection(); String encoding = Base64.getEncoder().encodeToString("username:password".getBytes(StandardCharsets.UTF_8)); con.setRequestProperty("Authorization","Basic "+encoding.replaceAll("\n", ""));
HttpsURLConnection con = null; con = (HttpsURLConnection) obj.openConnection(); 字符串编码 = Base64.getEncoder().encodeToString("username:password".getBytes(StandardCharsets.UTF_8)); con.setRequestProperty("授权","基本"+encoding.replaceAll("\n",""));
回答by Jim Jones
I found that the illegal character was caused by "Authorization: Basic ", encoded which should be "Authorization", "Basic " + encoded
我发现非法字符是由“授权:基本”引起的,编码应该是“授权”,“基本”+编码

