Connecting to remote URL which requires authentication using Java

前端 未结 12 2090
庸人自扰
庸人自扰 2020-11-22 12:59

How do I connect to a remote URL in Java which requires authentication. I\'m trying to find a way to modify the following code to be able to programatically provide a userna

相关标签:
12条回答
  • 2020-11-22 13:25

    You can set the default authenticator for http requests like this:

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

    Also, if you require more flexibility, you can check out the Apache HttpClient, which will give you more authentication options (as well as session support, etc.)

    0 讨论(0)
  • 2020-11-22 13:28

    As i have came here looking for an Android-Java-Answer i am going to do a short summary:

    1. Use java.net.Authenticator as shown by James van Huis
    2. Use Apache Commons HTTP Client, as in this Answer
    3. Use basic java.net.URLConnection and set the Authentication-Header manually like shown here

    If you want to use java.net.URLConnection with Basic Authentication in Android try this code:

    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
    
    0 讨论(0)
  • 2020-11-22 13:29

    Was able to set the auth using the 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);
    

    few of the changes fetched from this post. and Base64 is from java.util package.

    0 讨论(0)
  • 2020-11-22 13:33

    There's a native and less intrusive alternative, which works only for your call.

    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();
    
    0 讨论(0)
  • 2020-11-22 13:34

    You can also use the following, which does not require using external packages:

    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 讨论(0)
  • 2020-11-22 13:35

    Since Java 9, you can do this

    URL url = new URL("http://www.example.com");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setAuthenticator(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication ("USER", "PASS".toCharArray());
        }
    });
    
    0 讨论(0)
提交回复
热议问题