Class HttpClient


  • public class HttpClient
    extends java.lang.Object
    Send HTTP GET/POST requests. The main problem with current implementation is that it does not handle connections release. You must rely on the OS to release them (timeout).
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpClient()
      Creates an empty HttpClient object.
      HttpClient​(java.lang.String url)
      Creates a new HttpClient object.
      HttpClient​(java.lang.String url, java.util.Map<java.lang.String,​java.lang.Object> parameters)
      Creates a new HttpClient object.
      HttpClient​(java.lang.String url, java.util.Map<java.lang.String,​java.lang.Object> parameters, java.util.Map<java.lang.String,​java.lang.String> headers)
      Creates a new HttpClient object.
      HttpClient​(java.net.URL url)
      Creates a new HttpClient object.
      HttpClient​(java.net.URL url, java.util.Map<java.lang.String,​java.lang.Object> parameters)
      Creates a new HttpClient object.
      HttpClient​(java.net.URL url, java.util.Map<java.lang.String,​java.lang.Object> parameters, java.util.Map<java.lang.String,​java.lang.String> headers)
      Creates a new HttpClient object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static int checkHttpRequest​(java.lang.String url)  
      void followRedirects​(boolean followRedirects)
      Enables this request to follow redirect 3xx codes (default true)
      java.lang.String get()
      Invoke HTTP request GET.
      boolean getAllowUntrusted()
      Do we trust any certificate
      java.lang.String getClientCertificateAlias()
      Returns the alias of the client certificate to be used for this SSL connection.
      java.lang.String getContentType()
      Returns the content type
      java.util.Map<java.lang.String,​java.lang.String> getHeaders()
      Return a Map of headers.
      int getHostVerificationLevel()
      Returns the current server hostname verification level
      boolean getKeepAlive()
      Return keep-alive setting
      java.util.Map<java.lang.String,​java.lang.Object> getParameters()
      Return a Map of parameters.
      int getResponseCode()
      Gets response code.
      java.lang.Object getResponseContent()
      Returns the content of the response.
      java.lang.String getResponseContentEncoding()
      Returns the content encoding of the response.
      int getResponseContentLength()
      Returns the content length of the response
      java.lang.String getResponseContentType()
      Returns the content-type of the response.
      java.lang.String getResponseHeader​(java.lang.String header)
      Returns the value of the specified named response header field.
      java.lang.String getResponseHeaderField​(int n)
      Returns the value for the nth response header field.
      java.lang.String getResponseHeaderFieldKey​(int n)
      Returns the key for the nth response header field.
      java.io.InputStream getStream()
      Invoke HTTP request GET.
      java.lang.String getStreamCharset()
      Returns the stream charset
      java.lang.String getUrl()
      Return a string representing the requested URL.
      static java.lang.String getUrlContent​(java.lang.String url)  
      java.lang.String post()
      Invoke HTTP request POST.
      java.lang.String post​(java.lang.String stream)
      Invoke HTTP request POST and pass raw stream.
      java.io.InputStream postStream()
      Invoke HTTP request POST.
      java.lang.String sendHttpRequest​(java.lang.String method)
      Send http request string.
      void setAllowUntrusted​(boolean trustAny)
      Allow untrusted server certificates
      void setBasicAuthInfo​(java.lang.String basicAuthUsername, java.lang.String basicAuthPassword)
      Sets basic auth info.
      void setClientCertificateAlias​(java.lang.String alias)
      Sets the client certificate alias (from the keystore) to use for this SSL connection.
      void setContentType​(java.lang.String contentType)
      Sets the content-type
      void setDebug​(boolean debug)
      When true overrides Debug.verboseOn() and forces debugging for this instance
      void setHeader​(java.lang.String name, java.lang.String value)
      Set an individual header for this request.
      void setHeaders​(java.util.Map<java.lang.String,​java.lang.String> headers)
      Set the headers for this request.
      void setHostVerificationLevel​(int level)
      Sets the server hostname verification level
      void setKeepAlive​(boolean keepAlive)
      Toggle keep-alive setting
      void setLineFeed​(boolean lineFeed)
      Turns on or off line feeds in the request.
      void setParameter​(java.lang.String name, java.lang.String value)
      Set an individual parameter for this request.
      void setParameters​(java.util.Map<java.lang.String,​java.lang.Object> parameters)
      Set the parameters for this request.
      void setRawStream​(java.lang.String stream)
      Set the raw stream for posts.
      void setStreamCharset​(java.lang.String streamCharset)
      Sets the scream charset
      void setTimeout​(int timeout)
      Sets the timeout for waiting for the connection (default 30sec)
      void setUrl​(java.lang.String url)
      Set the URL for this request.
      void setUrl​(java.net.URL url)
      Set the URL for this request.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HttpClient

        public HttpClient()
        Creates an empty HttpClient object.
      • HttpClient

        public HttpClient​(java.net.URL url)
        Creates a new HttpClient object.
      • HttpClient

        public HttpClient​(java.lang.String url)
        Creates a new HttpClient object.
      • HttpClient

        public HttpClient​(java.lang.String url,
                          java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Creates a new HttpClient object.
      • HttpClient

        public HttpClient​(java.net.URL url,
                          java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Creates a new HttpClient object.
      • HttpClient

        public HttpClient​(java.lang.String url,
                          java.util.Map<java.lang.String,​java.lang.Object> parameters,
                          java.util.Map<java.lang.String,​java.lang.String> headers)
        Creates a new HttpClient object.
      • HttpClient

        public HttpClient​(java.net.URL url,
                          java.util.Map<java.lang.String,​java.lang.Object> parameters,
                          java.util.Map<java.lang.String,​java.lang.String> headers)
        Creates a new HttpClient object.
    • Method Detail

      • setDebug

        public void setDebug​(boolean debug)
        When true overrides Debug.verboseOn() and forces debugging for this instance
      • setTimeout

        public void setTimeout​(int timeout)
        Sets the timeout for waiting for the connection (default 30sec)
      • followRedirects

        public void followRedirects​(boolean followRedirects)
        Enables this request to follow redirect 3xx codes (default true)
      • setLineFeed

        public void setLineFeed​(boolean lineFeed)
        Turns on or off line feeds in the request. (default is on)
      • setRawStream

        public void setRawStream​(java.lang.String stream)
        Set the raw stream for posts.
      • setUrl

        public void setUrl​(java.net.URL url)
        Set the URL for this request.
      • setUrl

        public void setUrl​(java.lang.String url)
        Set the URL for this request.
      • setParameters

        public void setParameters​(java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Set the parameters for this request.
      • setParameter

        public void setParameter​(java.lang.String name,
                                 java.lang.String value)
        Set an individual parameter for this request.
      • setHeaders

        public void setHeaders​(java.util.Map<java.lang.String,​java.lang.String> headers)
        Set the headers for this request.
      • setHeader

        public void setHeader​(java.lang.String name,
                              java.lang.String value)
        Set an individual header for this request.
      • getHeaders

        public java.util.Map<java.lang.String,​java.lang.String> getHeaders()
        Return a Map of headers.
      • getParameters

        public java.util.Map<java.lang.String,​java.lang.Object> getParameters()
        Return a Map of parameters.
      • getUrl

        public java.lang.String getUrl()
        Return a string representing the requested URL.
      • setContentType

        public void setContentType​(java.lang.String contentType)
        Sets the content-type
      • getContentType

        public java.lang.String getContentType()
        Returns the content type
      • setStreamCharset

        public void setStreamCharset​(java.lang.String streamCharset)
        Sets the scream charset
      • getStreamCharset

        public java.lang.String getStreamCharset()
        Returns the stream charset
      • setKeepAlive

        public void setKeepAlive​(boolean keepAlive)
        Toggle keep-alive setting
      • getKeepAlive

        public boolean getKeepAlive()
        Return keep-alive setting
      • setClientCertificateAlias

        public void setClientCertificateAlias​(java.lang.String alias)
        Sets the client certificate alias (from the keystore) to use for this SSL connection.
      • getClientCertificateAlias

        public java.lang.String getClientCertificateAlias()
        Returns the alias of the client certificate to be used for this SSL connection.
      • setHostVerificationLevel

        public void setHostVerificationLevel​(int level)
        Sets the server hostname verification level
      • getHostVerificationLevel

        public int getHostVerificationLevel()
        Returns the current server hostname verification level
      • setAllowUntrusted

        public void setAllowUntrusted​(boolean trustAny)
        Allow untrusted server certificates
      • getAllowUntrusted

        public boolean getAllowUntrusted()
        Do we trust any certificate
      • setBasicAuthInfo

        public void setBasicAuthInfo​(java.lang.String basicAuthUsername,
                                     java.lang.String basicAuthPassword)
        Sets basic auth info.
        Parameters:
        basicAuthUsername - the basic auth username
        basicAuthPassword - the basic auth password
      • getResponseHeader

        public java.lang.String getResponseHeader​(java.lang.String header)
                                           throws HttpClientException
        Returns the value of the specified named response header field.
        Throws:
        HttpClientException
      • getResponseHeaderFieldKey

        public java.lang.String getResponseHeaderFieldKey​(int n)
                                                   throws HttpClientException
        Returns the key for the nth response header field.
        Throws:
        HttpClientException
      • getResponseHeaderField

        public java.lang.String getResponseHeaderField​(int n)
                                                throws HttpClientException
        Returns the value for the nth response header field. It returns null of there are fewer then n fields.
        Throws:
        HttpClientException
      • getResponseContent

        public java.lang.Object getResponseContent()
                                            throws java.io.IOException,
                                                   HttpClientException
        Returns the content of the response.
        Throws:
        java.io.IOException
        HttpClientException
      • sendHttpRequest

        public java.lang.String sendHttpRequest​(java.lang.String method)
                                         throws HttpClientException
        Send http request string.
        Parameters:
        method - the method
        Returns:
        the string
        Throws:
        HttpClientException - the http client exception