Package org.apache.ofbiz.base.util
Class HttpClient
- java.lang.Object
-
- org.apache.ofbiz.base.util.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).
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
module
-
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 certificatejava.lang.String
getClientCertificateAlias()
Returns the alias of the client certificate to be used for this SSL connection.java.lang.String
getContentType()
Returns the content typejava.util.Map<java.lang.String,java.lang.String>
getHeaders()
Return a Map of headers.int
getHostVerificationLevel()
Returns the current server hostname verification levelboolean
getKeepAlive()
Return keep-alive settingjava.util.Map<java.lang.String,java.lang.Object>
getParameters()
Return a Map of parameters.int
getResponseCode()
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 responsejava.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 charsetjava.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)
void
setAllowUntrusted(boolean trustAny)
Allow untrusted server certificatesvoid
setBasicAuthInfo(java.lang.String basicAuthUsername, java.lang.String basicAuthPassword)
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-typevoid
setDebug(boolean debug)
When true overrides Debug.verboseOn() and forces debugging for this instancevoid
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 levelvoid
setKeepAlive(boolean keepAlive)
Toggle keep-alive settingvoid
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 charsetvoid
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.
-
-
-
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)
-
get
public java.lang.String get() throws HttpClientException
Invoke HTTP request GET.- Throws:
HttpClientException
-
getStream
public java.io.InputStream getStream() throws HttpClientException
Invoke HTTP request GET.- Throws:
HttpClientException
-
post
public java.lang.String post() throws HttpClientException
Invoke HTTP request POST.- Throws:
HttpClientException
-
post
public java.lang.String post(java.lang.String stream) throws HttpClientException
Invoke HTTP request POST and pass raw stream.- Throws:
HttpClientException
-
postStream
public java.io.InputStream postStream() throws HttpClientException
Invoke HTTP request POST.- Throws:
HttpClientException
-
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
-
getResponseContentType
public java.lang.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 java.lang.String getResponseContentEncoding() throws HttpClientException
Returns the content encoding of the response.- Throws:
HttpClientException
-
getResponseCode
public int getResponseCode() throws HttpClientException
- Throws:
HttpClientException
-
sendHttpRequest
public java.lang.String sendHttpRequest(java.lang.String method) throws HttpClientException
- Throws:
HttpClientException
-
getUrlContent
public static java.lang.String getUrlContent(java.lang.String url) throws HttpClientException
- Throws:
HttpClientException
-
checkHttpRequest
public static int checkHttpRequest(java.lang.String url) throws HttpClientException
- Throws:
HttpClientException
-
-