Java的:与HTTPBasic认证获取网址 [英] Java: fetch URL with HTTPBasic Authentication
问题描述
我在做一些简单的HTTP认证和我得到一个
java.lang.IllegalArgumentException:如果邮件标头值非法字符(S):基本OGU0ZTc5ODBk(...从76个字符修剪...)
(...更多密码数据......)
我想这是因为我有一个很长的用户名和密码,连接codeR用其包装\\ n
76个字符。有没有什么办法可以解决这个问题?该URL只支持HTTP基本认证。
下面是我的code:
私有类UserPassAuthenticator扩展验证器{
字符串的用户;
字符串传递;
公共UserPassAuthenticator(用户字符串,字符串通){
this.user =用户;
this.pass =传递;
} //此方法时,密码保护的URL访问称为
受保护的PasswordAuthentication的getPasswordAuthentication(){
返回新的PasswordAuthentication(用户,pass.toCharArray());
}
}私人字符串取(StoreAccount帐户,字符串路径)抛出IOException
Authenticator.setDefault(新UserPassAuthenticator(account.getCredentials()getLogin(),account.getCredentials()getPassword来())。); 网址URL =新的URL(https开头,account.getStoreUrl()取代(的http://,),路径);
的System.out.println(URL); URLConnection的urlConn = url.openConnection();
对象o = urlConn.getContent();
如果(!(邻的instanceof字符串))
扔(关于错误的Content-Type+ url.toString())的新IOException异常; //删除认证回默认
Authenticator.setDefault(NULL);
返回(字符串)O;
}
这似乎是在错误Java的的。
您是否尝试过使用替代HTTP客户端,如Apache的图书馆吗?
或代替使用验证器,手动设置标题?
网址URL =新的URL(http://www.example.com/);
HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
connection.setRequestProperty(授权,基本OGU0ZTc5ODBkABcde ......);
令牌值为en codeBase64(用户名:密码)。
I'm doing some simple HTTP authentication and am getting a
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 \n
at 76 chars. Is there any way I can get around this? The URL only supports HTTP Basic Auth.
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;
}
That seems to be a bug in Java.
Have you tried using alternative HTTP clients, such as the library from 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").
这篇关于Java的:与HTTPBasic认证获取网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!