Any method to get constant for HTTP and HTTPS values

Viewed 1119

Is there a class whene HTTP and HTTPs constant value is defined. As of now I have created a custom class.

   public static class UrlConstants {

    private UrlConstants() {
        super();
    }

    /** The Constant HTTP. */
    public static final String HTTP = "http";
}
3 Answers

A little clunky, but these work.

String.valueOf(org.apache.commons.httpclient.HttpURL.DEFAULT_SCHEME) String.valueOf(org.apache.commons.httpclient.HttpsURL.DEFAULT_SCHEME)

There's also org.mortbay.jetty.HttpSchemes.HTTP and org.mortbay.jetty.HttpSchemes.HTTPS.

As far as I can tell (as of JDK 8 which I have handy), the best you can do within the Java JDK is

javax.print.attribute.standard.ReferenceUriSchemesSupported

which provides a number of URI-Schemes:

import javax.print.attribute.standard.ReferenceUriSchemesSupported;
// ...
System.out.println(ReferenceUriSchemesSupported.HTTPS); // => "https"

Of course, it also has GOPHER, NNTP, NEWS, and a few other out-of-date schemes, but it does provide what you want.

Other answers on the web suggest other frameworks, many of which do define the two protocols.

Otherwise, you just need something like this:

package demo;

public enum Scheme {
    HTTP("http"),
    HTTPS("https");
    private final String scheme;

    private Scheme(String scheme) {
        this.scheme = scheme;
    }

    @Override
    public String toString() {
        return scheme;
    }
}
HttpStatus.OK.value() will return the integer value of the status code.

Probably this will help your query, this is from org.springframework.http package

Related