use*_*128 119 java httpurlconnection
如何连接到需要身份验证的Java远程URL.我试图找到一种方法来修改以下代码,以便能够以编程方式提供用户名/密码,因此它不会抛出401.
URL url = new URL(String.format("http://%s/manager/list", _host + ":8080"));
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
Run Code Online (Sandbox Code Playgroud)
Jam*_*uis 127
您可以为http请求设置默认验证器,如下所示:
Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication ("username", "password".toCharArray());
}
});
Run Code Online (Sandbox Code Playgroud)
此外,如果您需要更多灵活性,可以查看Apache HttpClient,它将为您提供更多身份验证选项(以及会话支持等)
Wan*_*tos 127
这是一种原生的,不那么具有侵入性的选择,仅适用于您的通话.
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();
Run Code Online (Sandbox Code Playgroud)
小智 77
您还可以使用以下内容,不需要使用外部包:
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();
Run Code Online (Sandbox Code Playgroud)
jav*_*der 39
If you are using the normal login whilst entering the username and password between the protocol and the domain this is simpler. It also works with and without login.
Sample Url: http://user:pass@domain.com/url
URL url = new URL("http://user:pass@domain.com/url");
URLConnection urlConnection = url.openConnection();
if (url.getUserInfo() != null) {
String basicAuth = "Basic " + new String(new Base64().encode(url.getUserInfo().getBytes()));
urlConnection.setRequestProperty("Authorization", basicAuth);
}
InputStream inputStream = urlConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)
当我来到这里寻找Android-Java-Answer时,我将做一个简短的总结:
如果你想在Android中使用带有基本身份验证的java.net.URLConnection,请尝试以下代码:
URL url = new URL("http://www.mywebsite.com/resource");
URLConnection urlConnection = url.openConnection();
String header = "Basic " + new String(android.util.Base64.encode("user:pass".getBytes(), android.util.Base64.NO_WRAP));
urlConnection.addRequestProperty("Authorization", header);
// go on setting more request headers, reading the response, etc
Run Code Online (Sandbox Code Playgroud)
能够使用 HttpsURLConnection 设置身份验证
URL myUrl = new URL(httpsURL);
HttpsURLConnection conn = (HttpsURLConnection)myUrl.openConnection();
String userpass = username + ":" + password;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
//httpsurlconnection
conn.setRequestProperty("Authorization", basicAuth);
Run Code Online (Sandbox Code Playgroud)
从这篇文章中提取的更改很少。Base64 来自 java.util 包。
使用“Base64().encode()”方法时要非常小心,我和我的团队遇到了 400 个 Apache 错误请求问题,因为它在生成的字符串末尾添加了 \r\n。
感谢 Wireshark,我们发现它可以嗅探数据包。
这是我们的解决方案:
import org.apache.commons.codec.binary.Base64;
HttpGet getRequest = new HttpGet(endpoint);
getRequest.addHeader("Authorization", "Basic " + getBasicAuthenticationEncoding());
private String getBasicAuthenticationEncoding() {
String userPassword = username + ":" + password;
return new String(Base64.encodeBase64(userPassword.getBytes()));
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!