Home >Java >javaTutorial >How to Authenticate Remote URL Access in Java?
Requesting Authentication for Remote URL Access in Java
Connecting to a remote URL that requires authentication can be challenging. A common issue arises when attempting to connect to a URL using code like the given example:
URL url = new URL(String.format("http://%s/manager/list", _host + ":8080")); HttpURLConnection connection = (HttpURLConnection)url.openConnection();
Without proper authentication, this code may encounter a 401 error. To address this situation, we can modify the code to provide a username and password програмmatically:
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();
In this modified version:
The above is the detailed content of How to Authenticate Remote URL Access in Java?. For more information, please follow other related articles on the PHP Chinese website!