更新时间:2022-06-25 09:43:13
您需要输出流吗?HttpURLConnection
绝对支持使用 Authenticator
类进行身份验证,请参阅:Http 身份验证.
Do you need output streaming? The HttpURLConnection
most definitely supports authentication with the Authenticator
class, see: Http Authentication.
更新:如果 Authenticator
不是一个选项,您可以通过向您的 HTTP 请求添加一个额外的标头来手动进行 HTTP 基本身份验证.尝试以下代码(未经测试):
Update: In case the Authenticator
is not an option, you can manually do HTTP basic authentication by adding an extra header to your HTTP request. Try the following code (untested):
String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", "Basic " + encoding);
uc.connect();