连接到需要使用Java进行身份验证的远程URL如何连接到需要身份验证的Java远程URL。我试图找到一种方法来修改下面的代码,以便能够编程地提供用户名/密码,这样它就不会抛出401。URL url = new URL(String.format("http://%s/manager/list", _host + ":8080"));
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
3 回答

慕运维8079593
TA贡献1876条经验 获得超5个赞
Authenticator.setDefault (new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication ("username", "password".toCharArray()); }});

慕盖茨4494581
TA贡献1850条经验 获得超11个赞
URL url = new URL(“location address”);URLConnection uc = url.openConnection();String userpass = username + ":" + password;String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));uc.setRequestProperty ("Authorization", basicAuth);InputStream in = uc.getInputStream();

猛跑小猪
TA贡献1858条经验 获得超8个赞
URL url = new URL(“location address”);URLConnection uc = url.openConnection();String userpass = username + ":" + password;String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); uc.setRequestProperty ("Authorization", basicAuth);InputStream in = uc.getInputStream();
添加回答
举报
0/150
提交
取消