401的意思是“未经授权”,因此您的凭据中一定有东西。
我认为Java
URL不支持您显示的语法。您可以改用Authenticator。
Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(login, password.toCharArray()); }});然后只需调用常规网址即可,无需提供凭据。
另一个选项是在标头中提供凭据:
String loginPassword = login+ ":" + password;String enpred = new sun.misc.base64Enprer().enpre (loginPassword.getBytes());URLConnection conn = url.openConnection();conn.setRequestProperty ("Authorization", "Basic " + enpred);PS:不建议使用该base64Enprer,但这只是为了显示快速解决方案。如果您想保留该解决方案,请寻找可以使用的库。有很多。



