Class HttpClient

java.lang.Object
org.apache.ofbiz.base.util.HttpClient

public class HttpClient extends 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 Details

    • HttpClient

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

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

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

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

      public HttpClient(URL url, Map<String,Object> parameters)
      Creates a new HttpClient object.
    • HttpClient

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

      public HttpClient(URL url, Map<String,Object> parameters, Map<String,String> headers)
      Creates a new HttpClient object.
  • Method Details

    • 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(String stream)
      Set the raw stream for posts.
    • setUrl

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

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

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

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

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

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

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

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

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

      public void setContentType(String contentType)
      Sets the content-type
    • getContentType

      public String getContentType()
      Returns the content type
    • setStreamCharset

      public void setStreamCharset(String streamCharset)
      Sets the scream charset
    • getStreamCharset

      public 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(String alias)
      Sets the client certificate alias (from the keystore) to use for this SSL connection.
    • getClientCertificateAlias

      public 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(String basicAuthUsername, String basicAuthPassword)
      Sets basic auth info.
      Parameters:
      basicAuthUsername - the basic auth username
      basicAuthPassword - the basic auth password
    • get

      public String get() throws HttpClientException
      Invoke HTTP request GET.
      Throws:
      HttpClientException
    • getStream

      public InputStream getStream() throws HttpClientException
      Invoke HTTP request GET.
      Throws:
      HttpClientException
    • post

      public String post() throws HttpClientException
      Invoke HTTP request POST.
      Throws:
      HttpClientException
    • post

      public String post(String stream) throws HttpClientException
      Invoke HTTP request POST and pass raw stream.
      Throws:
      HttpClientException
    • postStream

      public InputStream postStream() throws HttpClientException
      Invoke HTTP request POST.
      Throws:
      HttpClientException
    • getResponseHeader

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

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

      public 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 Object getResponseContent() throws IOException, HttpClientException
      Returns the content of the response.
      Throws:
      IOException
      HttpClientException
    • getResponseContentType

      public String getResponseContentType() throws HttpClientException
      Returns the content-type of the response.
      Throws:
      HttpClientException
    • getResponseContentLength

      public int getResponseContentLength() throws HttpClientException
      Returns the content length of the response
      Throws:
      HttpClientException
    • getResponseContentEncoding

      public String getResponseContentEncoding() throws HttpClientException
      Returns the content encoding of the response.
      Throws:
      HttpClientException
    • getResponseCode

      public int getResponseCode() throws HttpClientException
      Gets response code.
      Returns:
      the response code
      Throws:
      HttpClientException - the http client exception
    • sendHttpRequest

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

      public static String getUrlContent(String url) throws HttpClientException
      Throws:
      HttpClientException
    • checkHttpRequest

      public static int checkHttpRequest(String url) throws HttpClientException
      Throws:
      HttpClientException