连接到需要使用Java进行身份validation的远程URL

如何连接到需要validation的Java中的远程URL。 我试图find一种方法来修改下面的代码,以编程方式提供用户名/密码,所以它不会扔401。

URL url = new URL(String.format("http://%s/manager/list", _host + ":8080")); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 

您可以为http请求设置默认的身份validation器,如下所示:

 Authenticator.setDefault (new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication ("username", "password".toCharArray()); } }); 

另外,如果你需要更多的灵活性,你可以查看Apache HttpClient ,它会给你更多的authentication选项(以及会话支持等)

有一个本地和较less侵入的select,只适用于您的电话。

 URL url = new URL(“location address”); URLConnection uc = url.openConnection(); String userpass = username + ":" + password; String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes())); uc.setRequestProperty ("Authorization", basicAuth); InputStream in = uc.getInputStream(); 

您还可以使用以下内容,而不需要使用外部程序包:

 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(); 

如果您在使用协议和域名之间input用户名和密码时使用正常login,则更简单。 它也适用于和不用login。

样本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(); 

正如我来到这里寻找一个Android的Java – 答案我要做一个简短的总结:

  1. 使用James van Huis所示的java.net.Authenticator
  2. 如本答案中所述 ,使用Apache Commons HTTP Client
  3. 使用基本的java.net.URLConnection并手动设置Authentication-Header,如下所示

如果你想在Android中使用带基本authentication的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 

要小心“Base64()。encode()”方法,我的团队和我有400个Apache请求的问题,因为它在生成的string的末尾添加了\ r \ n。

感谢Wireshark,我们发现它嗅探数据包。

这是我们的解决scheme:

 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())); } 

希望它有帮助!

使用此代码进行基本身份validation。

 URL url = new URL(path); String userPass = "username:password"; String basicAuth = "Basic " + Base64.encodeToString(userPass.getBytes(), Base64.DEFAULT);//or //String basicAuth = "Basic " + new String(Base64.encode(userPass.getBytes(), Base64.No_WRAP)); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); urlConnection.setRequestProperty("Authorization", basicAuth); urlConnection.connect(); 

ANDROD IMPLEMENTATION一个完整的方法,用于请求使用用户名和密码进行授权的Web服务请求数据/string响应

 public static String getData(String uri, String userName, String userPassword) { BufferedReader reader = null; byte[] loginBytes = (userName + ":" + userPassword).getBytes(); StringBuilder loginBuilder = new StringBuilder() .append("Basic ") .append(Base64.encodeToString(loginBytes, Base64.DEFAULT)); try { URL url = new URL(uri); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.addRequestProperty("Authorization", loginBuilder.toString()); StringBuilder sb = new StringBuilder(); reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine())!= null){ sb.append(line); sb.append("\n"); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); return null; } finally { if (null != reader){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }