Class HttpClient

  • Direct Known Subclasses:
    Http1Client

    public abstract class HttpClient
    extends java.lang.Object
    Provides uniform access for a HTTP/HTTPS server. This class should be used in favour of HttpURLConnection.
    Since:
    9168
    • Constructor Detail

      • HttpClient

        protected HttpClient​(java.net.URL url,
                             java.lang.String requestMethod)
        Constructs a new HttpClient.
        Parameters:
        url - URL to access
        requestMethod - HTTP request method (GET, POST, PUT, DELETE...)
    • Method Detail

      • connect

        public final HttpClient.Response connect()
                                          throws java.io.IOException
        Opens the HTTP connection.
        Returns:
        HTTP response
        Throws:
        java.io.IOException - if any I/O error occurs
      • connect

        public final HttpClient.Response connect​(ProgressMonitor progressMonitor)
                                          throws java.io.IOException
        Opens the HTTP connection.
        Parameters:
        progressMonitor - progress monitor
        Returns:
        HTTP response
        Throws:
        java.io.IOException - if any I/O error occurs
        Since:
        9179
      • connect

        public final HttpClient.Response connect​(ProgressMonitor progressMonitor,
                                                 java.lang.String authRedirectLocation,
                                                 java.lang.String authRequestProperty)
                                          throws java.io.IOException
        Opens the HTTP connection.
        Parameters:
        progressMonitor - progress monitor
        authRedirectLocation - The location where we will be redirected for authentication
        authRequestProperty - The authorization header to set when being redirected to the auth location
        Returns:
        HTTP response
        Throws:
        java.io.IOException - if any I/O error occurs
        Since:
        18913
      • setupConnection

        protected abstract void setupConnection​(ProgressMonitor progressMonitor)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • performDisconnection

        protected abstract void performDisconnection()
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • logRequest

        protected final void logRequest​(java.lang.String pattern,
                                        java.lang.Object... args)
      • getResponse

        public final HttpClient.Response getResponse()
        Returns the HTTP response which is set only after calling connect(). Calling this method again, returns the identical object (unless another connect() is performed).
        Returns:
        the HTTP response
        Since:
        9309
      • create

        public static HttpClient create​(java.net.URL url)
        Creates a new instance for the given URL and a GET request
        Parameters:
        url - the URL
        Returns:
        a new instance
      • create

        public static HttpClient create​(java.net.URL url,
                                        java.lang.String requestMethod)
        Creates a new instance for the given URL and a GET request
        Parameters:
        url - the URL
        requestMethod - the HTTP request method to perform when calling
        Returns:
        a new instance
      • getRequestBody

        public final byte[] getRequestBody()
        Returns the request body set for this connection.
        Returns:
        the HTTP request body, or null
        Since:
        15229
      • hasRequestBody

        public final boolean hasRequestBody()
        Determines if a non-empty request body has been set for this connection.
        Returns:
        true if the request body is set and non-empty
        Since:
        15229
      • requiresBody

        public final boolean requiresBody()
        Determines if the underlying HTTP method requires a body.
        Returns:
        true if the underlying HTTP method requires a body
        Since:
        15229
      • getRequestMethod

        public final java.lang.String getRequestMethod()
        Returns the request method set for this connection.
        Returns:
        the HTTP request method
        Since:
        9172
        See Also:
        create(URL, String)
      • getRequestHeader

        public final java.lang.String getRequestHeader​(java.lang.String header)
        Returns the set value for the given header.
        Parameters:
        header - HTTP header name
        Returns:
        HTTP header value
        Since:
        9172
      • getConnectTimeout

        public final int getConnectTimeout()
        Returns the connect timeout.
        Returns:
        the connect timeout, in milliseconds
        Since:
        15229
      • getReadTimeout

        public final int getReadTimeout()
        Returns the read timeout.
        Returns:
        the read timeout, in milliseconds
        Since:
        15229
      • getIfModifiedSince

        public final long getIfModifiedSince()
        Returns the If-Modified-Since header value.
        Returns:
        the If-Modified-Since header value
        Since:
        15229
      • isUseCache

        public final boolean isUseCache()
        Determines whether not to set header Cache-Control=no-cache. By default, useCache is true, i.e., the header Cache-Control=no-cache is not sent.
        Returns:
        whether not to set header Cache-Control=no-cache
        Since:
        15229
      • getHeaders

        public final java.util.Map<java.lang.String,​java.lang.String> getHeaders()
        Returns the headers.
        Returns:
        the headers
        Since:
        15229
      • getReasonForRequest

        public final java.lang.String getReasonForRequest()
        Returns the reason for request.
        Returns:
        the reason for request
        Since:
        15229
      • getOutputMessage

        protected final java.lang.String getOutputMessage()
        Returns the output message.
        Returns:
        the output message
      • isFinishOnCloseOutput

        protected final boolean isFinishOnCloseOutput()
        Determines whether the progress monitor task will be finished when the output stream is closed. true by default.
        Returns:
        the finishOnCloseOutput
      • useCache

        public final HttpClient useCache​(boolean useCache)
        Sets whether not to set header Cache-Control=no-cache. By default, useCache is true, i.e., the header Cache-Control=no-cache is not sent.
        Parameters:
        useCache - whether not to set header Cache-Control=no-cache
        Returns:
        this
        See Also:
        URLConnection.setUseCaches(boolean)
      • keepAlive

        public final HttpClient keepAlive​(boolean keepAlive)
        Sets whether not to set header Connection=close

        This might fix #7640, see here.

        Parameters:
        keepAlive - whether not to set header Connection=close
        Returns:
        this
      • setConnectTimeout

        public final HttpClient setConnectTimeout​(int connectTimeout)
        Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
        Parameters:
        connectTimeout - an int that specifies the connect timeout value in milliseconds
        Returns:
        this
        See Also:
        URLConnection.setConnectTimeout(int)
      • setReadTimeout

        public final HttpClient setReadTimeout​(int readTimeout)
        Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.
        Parameters:
        readTimeout - an int that specifies the read timeout value in milliseconds
        Returns:
        this
        See Also:
        URLConnection.setReadTimeout(int)
      • setAccept

        public final HttpClient setAccept​(java.lang.String accept)
        Sets the Accept header.
        Parameters:
        accept - header value
        Returns:
        this
      • setRequestBody

        public final HttpClient setRequestBody​(byte[] requestBody)
        Sets the request body for PUT/POST requests.
        Parameters:
        requestBody - request body
        Returns:
        this
      • setIfModifiedSince

        public final HttpClient setIfModifiedSince​(long ifModifiedSince)
        Sets the If-Modified-Since header.
        Parameters:
        ifModifiedSince - header value
        Returns:
        this
      • setMaxRedirects

        public final HttpClient setMaxRedirects​(int maxRedirects)
        Sets the maximum number of redirections to follow. Set maxRedirects to -1 in order to ignore redirects, i.e., to not throw an IOException in connect().
        Parameters:
        maxRedirects - header value
        Returns:
        this
      • setHeader

        public final HttpClient setHeader​(java.lang.String key,
                                          java.lang.String value)
        Sets an arbitrary HTTP header.
        Parameters:
        key - header name
        value - header value
        Returns:
        this
      • setHeaders

        public final HttpClient setHeaders​(java.util.Map<java.lang.String,​java.lang.String> headers)
        Sets arbitrary HTTP headers.
        Parameters:
        headers - HTTP headers
        Returns:
        this
      • setReasonForRequest

        public final HttpClient setReasonForRequest​(java.lang.String reasonForRequest)
        Sets a reason to show on console. Can be null if no reason is given.
        Parameters:
        reasonForRequest - Reason to show
        Returns:
        this
        Since:
        9172
      • setOutputMessage

        public final HttpClient setOutputMessage​(java.lang.String outputMessage)
        Sets the output message to be displayed in progress monitor for PUT, POST and DELETE methods. Defaults to "Uploading data ..." (translated). Has no effect for GET or any other method.
        Parameters:
        outputMessage - message to be displayed in progress monitor
        Returns:
        this
        Since:
        12711
      • setFinishOnCloseOutput

        public final HttpClient setFinishOnCloseOutput​(boolean finishOnCloseOutput)
        Sets whether the progress monitor task will be finished when the output stream is closed. This is true by default.
        Parameters:
        finishOnCloseOutput - whether the progress monitor task will be finished when the output stream is closed
        Returns:
        this
        Since:
        10302
      • setLogAtDebug

        public final HttpClient setLogAtDebug​(boolean debug)
        Sets the connect log at DEBUG level instead of the default INFO level.
        Parameters:
        debug - true to set the connect log at DEBUG level
        Returns:
        this
        Since:
        15389
      • isRedirect

        public static boolean isRedirect​(int statusCode)
        Determines if the given status code is an HTTP redirection.
        Parameters:
        statusCode - HTTP status code
        Returns:
        true if the given status code is an HTTP redirection
        Since:
        15423
      • disconnect

        public abstract void disconnect()
        Disconnect client.
        Since:
        9309
        See Also:
        HttpURLConnection.disconnect()
      • getTomcatErrorMatcher

        public static java.util.regex.Matcher getTomcatErrorMatcher​(java.lang.String data)
        Returns a Matcher against predefined Tomcat error messages. If it matches, error message can be extracted from group(1).
        Parameters:
        data - HTML contents to check
        Returns:
        a Matcher against predefined Tomcat error messages
        Since:
        13358