Package com.storedobject.common
Class HTTP
java.lang.Object
com.storedobject.common.HTTP
Simple HTTP utility. Requests can be sent multiple times but before sending subsequent requests the method
done()
should be invoked to clean up the previous request.
Note: This is written before HttpClient
was available in Java. So, it is better
to use that class and its related cousins instead of this class.
New: Now a new version of HTTP class is added for making more efficient connection and easier content
handling. See HTTP2
. This class utilizes the latest HttpClient
class in Java. I can
also handle chunked and/or compressed content automatically.
- Author:
- Syam
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the connection and associated resources.void
Disable SNI extension.void
done()
Invoke this method specify that the current request/response is processed and the resources associated with it are released.static String
Utility method to URL-encode some value.Get the connection to manipulate it directly.Get the input stream to read the response.Get the request method.Get the output stream to send the request directly.Get the response as a "reader".Get the output stream to send the request directly.void
Do a "post" request with the given parameter.void
Do a "post" request with the given parameters.read()
Read the response as a string.readJSON()
Read the response as a JSON.readXML()
Read the response as XML.void
saveTo
(OutputStream output) Save the response directly to an output stream.void
Set the connection in AJAX mode.void
setAllowHTTPErrors
(boolean allow) Allow HTTP errors.void
setBasicAuthentication
(String user, String password) Set basic authentication.void
Set basic authentication.void
setConnectTimeout
(int connectTimeout) Set the connection timeout for the HTTP request.void
setContentType
(String contentType) Set content type.void
setHostnameVerifier
(HostnameVerifier verifier) Set the host verifier for this connection.void
Set the content type to JSON ("application/json").void
Set request method ("GET", "POST", etc.)void
setReadTimeout
(int readTimeout) Set the read timeout for the connection.void
setRequestProperty
(String propertyName, String propertyValue) Set a request property directly.void
setSSLSocketFactory
(SSLSocketFactory socketFactory) Set custom SSL socket factory for this connection.
-
Constructor Details
-
HTTP
Create a connection. (By default, GET method will be used). Default content type is "application/x-www-form-urlencoded".- Parameters:
url
- URL.- Throws:
MalformedURLException
- If URL is invalid.
-
HTTP
Create a connection. Default content type is "application/x-www-form-urlencoded".- Parameters:
url
- URL.post
- Whether to use POST method or not.- Throws:
MalformedURLException
- If URL is invalid.
-
HTTP
Create a connection. (By default, GET method will be used). Default content type is "application/x-www-form-urlencoded".- Parameters:
url
- URL.
-
HTTP
Create a connection. Default content type is "application/x-www-form-urlencoded".- Parameters:
url
- URL.post
- Whether to use POST method or not.
-
-
Method Details
-
done
public void done()Invoke this method specify that the current request/response is processed and the resources associated with it are released. Further requests may be sent after this. If you don't want to do any further requests, rather than calling this method, it is better to invokeclose()
. -
setMethod
Set request method ("GET", "POST", etc.)- Parameters:
method
- The method to set.
-
getMethod
-
setAJAXMode
public void setAJAXMode()Set the connection in AJAX mode. -
setJSONMode
public void setJSONMode()Set the content type to JSON ("application/json"). -
setContentType
Set content type.- Parameters:
contentType
- Content type to set.
-
setBasicAuthentication
-
setBearerAuthentication
Set basic authentication.- Parameters:
key
- Bearer key.
-
setRequestProperty
-
setAllowHTTPErrors
public void setAllowHTTPErrors(boolean allow) Allow HTTP errors. If not allowed, an exception will be raised while raading the response.- Parameters:
allow
- Whether to allow or not.
-
getInputStream
Get the input stream to read the response. If HTTP errors are not allowed, an exception will be raised if the response is not HTTP OK. If HTTP errors are allowed, the "error stream" is returned.- Returns:
- Stream to read the response.
- Throws:
Exception
- If any error occurs.
-
getReader
Get the response as a "reader".- Returns:
- Get the response via a reader.
- Throws:
Exception
- If any error occurs. (SeegetInputStream()
).
-
getOutputStream
Get the output stream to send the request directly.- Returns:
- The output stream to which request can be written to.
- Throws:
IOException
- If any IO exception occurs.
-
getWriter
Get the output stream to send the request directly.- Returns:
- The output stream as a "writer" to which request can be written to.
- Throws:
IOException
- If any IO exception occurs.Exception
-
saveTo
Save the response directly to an output stream.- Parameters:
output
- Output stream to save the response body received.- Throws:
Exception
- If any exception occurs.
-
encode
-
post
Do a "post" request with the given parameters. If the JSON mode is on, the request will be sent as a JSON request, otherwise as parameters.- Parameters:
parameters
- Map containing parameters.- Throws:
Exception
- If any exception occurs.
-
post
-
read
-
readXML
-
readJSON
-
getConnection
Get the connection to manipulate it directly.- Returns:
- Connection.
- Throws:
Exception
- If any exception occurs.
-
close
public void close()Close the connection and associated resources. After this, no more requests may be sent. -
setHostnameVerifier
Set the host verifier for this connection.- Parameters:
verifier
- Host verifier.
-
setSSLSocketFactory
Set custom SSL socket factory for this connection.- Parameters:
socketFactory
- Factory to be set.
-
disableSNIExtension
public void disableSNIExtension()Disable SNI extension. By default, it's enabled.Note: This will disable SNI extension system-wide while connecting, and it will be enabled again.
-
setReadTimeout
public void setReadTimeout(int readTimeout) Set the read timeout for the connection. If the timeout is less than or equal to 0, the default timeout of 30000 milliseconds will be used.- Parameters:
readTimeout
- The read timeout in milliseconds.
-
setConnectTimeout
public void setConnectTimeout(int connectTimeout) Set the connection timeout for the HTTP request. If the timeout is less than or equal to 0, the default timeout of 30000 milliseconds will be used.- Parameters:
connectTimeout
- the connection timeout in milliseconds
-