Question

I am new to using REST and feel unconfident. So could you help and say what is the best approach to create a REST client to connect to server using SSL and authorization? I am not asking full example, just what library to use or maybe some native library?

Was it helpful?

Solution 2

You can use the built-in HttpURLConnection class to talk to a restful service. That class also supports https urls, which provide you with SSL encryption. For the authentication, you can use the built in mechanisms.

For Username+Password or Digest auth, you can just use the java.net.Authenticator, that you may need to extend like this:

public class AS7Authenticator extends Authenticator {

    private String user;
    private String pass;

    public AS7Authenticator(String user, String pass) {
        this.user = user;
        this.pass = pass;
        if (this.pass==null)
            this.pass=""; // prevent NPE later
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user,pass.toCharArray());
    }
}

(taken from the JBossAS7 plugin of RHQ)

For Android versions > 2.3, the HttpUrlConnection seems to be the preferred client by the Android developers anyway.

OTHER TIPS

In Java, my favorite HTTP client library is java.net.HttpURLConnection. It is already in the Java API; it handles cache, SSL, authentication.

Here is an example: https://github.com/Hypertopic/Porphyry/blob/master/src/org/hypertopic/RESTDatabase.java#L181

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top