Class HttpClient

    • Constructor Detail

      • HttpClient

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

      • 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​(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​(URL url,
                                        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
      • getRequestHeader

        public final String getRequestHeader​(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
      • getReasonForRequest

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

        protected final 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​(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​(String key,
                                          String value)
        Sets an arbitrary HTTP header.
        Parameters:
        key - header name
        value - header value
        Returns:
        this
      • setReasonForRequest

        public final HttpClient setReasonForRequest​(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​(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
      • getTomcatErrorMatcher

        public static Matcher getTomcatErrorMatcher​(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