public class HTTPRequest extends Object
InputStream to the
returned data.
All kind of parameters can be added to the request using
addParameter(String, Object).
Example:
HTTPRequest req = new HTTPRequest(url, HTTPRequest.POST);
req.addParameter("user", username);
req.addParameter("email", email);
req.doRequest();
Reader input = req.getContentReader();
All parameters will be sent UTF-8 encoded, as proposed by W3C.
GET requests are limited in their length. This class will not take care to keep within the allowed length. If you are in doubt, you should use POST request.
It is possible to pass a ProgressMonitor which will be used to show how many
data has been transfered to the server. This is especially useful for file uploads.
| Modifier and Type | Class and Description |
|---|---|
static interface |
HTTPRequest.DataProvider
A DataProvider provides a data stream to be sent by the
HTTPRequest. |
static class |
HTTPRequest.FileProvider
This
HTTPRequest.DataProvider implementation uploads a File to the server. |
static class |
HTTPRequest.InputStreamProvider
This
HTTPRequest.DataProvider implementation transmits the given InputStream. |
static class |
HTTPRequest.Method |
| Constructor and Description |
|---|
HTTPRequest(URL url)
Creates a new HTTPRequest using method GET.
|
HTTPRequest(URL url,
HTTPRequest.Method method)
Creates a new HTTPRequest.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addDataProvider(String name,
HTTPRequest.DataProvider provider)
Adds a
HTTPRequest.DataProvider to the HTTPRequest. |
void |
addFile(String name,
File file)
Adds a
File to the HTTPRequest. |
void |
addFile(String name,
File file,
String mimetype)
Adds a
File to the HTTPRequest. |
void |
addParameter(String name,
boolean value)
Convenience call to add a boolean parameter to the HTTPRequest.
|
void |
addParameter(String name,
Object value)
Adds a parameter to the HTTPRequest.
|
int |
doRequest()
Sends the request to the server.
|
String |
getCharset()
Gets the charset of the response.
|
HttpURLConnection |
getConnection()
Returns the
HttpURLConnection object after doRequest() has been
invoked. |
Reader |
getContentReader()
Returns a
Reader to the response body after a doRequest(). |
HTTPRequest.Method |
getMethod()
Returns the method this request is sent with.
|
InputStream |
getResult()
Returns the server's response in an
InputStream after a
doRequest(). |
boolean |
isPost()
Returns true if this request uses the POST method.
|
void |
setProgressMonitor(ProgressMonitor monitor,
String transfer)
Defines a
ProgressMonitor for the transmission process. |
void |
setProgressMonitor(ProgressMonitor monitor,
String transfer,
int min,
int max)
Defines a
ProgressMonitor for the transmission process. |
public HTTPRequest(URL url)
url - Target URL for the request.public HTTPRequest(URL url, HTTPRequest.Method method)
url - Target URL for the request.method - HTTPRequest.Method: GET or POSTpublic void addParameter(String name, Object value)
Object.toString() result of the
value is transferred to the server. The value is evaluated when
doRequest() is invoked, NOT at the time you pass the parameter here. Keep
this in mind!
If a null was passed as value, the parameter will not be sent at all.
Note that a parameter can only be passed once. If you add it again, it will replace the previous parameter with the same name. Also take care not to exceed the limits of a GET request. Use POST if you are in doubt.
HTTPRequest will take care for proper encoding of the parameter names and values. You won't need to urlencode them.
name - Parameter namevalue - Parameter value, may be nullpublic void addParameter(String name, boolean value)
name - Parameter namevalue - Parameter valuepublic void addFile(String name, File file) throws FileNotFoundException
File to the HTTPRequest. You must use POST requests in order to
upload files to the server. The file will be sent using the mime type
"application/octet-stream". The file is not kept in memory, but sent using a data
stream during transmission, so HTTPRequest is also able to upload large files with
a small memory print.name - Parameter namefile - File to be sentFileNotFoundExceptionpublic void addFile(String name, File file, String mimetype) throws FileNotFoundException
File to the HTTPRequest. You must use POST requests in order to
upload files to the server. The file is not kept in memory, but sent using a data
stream during transmission, so HTTPRequest is also able to upload large files.name - Parameter namefile - File to be sentmimetype - Mime type of the file to be transmittedFileNotFoundExceptionpublic void addDataProvider(String name, HTTPRequest.DataProvider provider)
HTTPRequest.DataProvider to the HTTPRequest. You must use POST requests in order
to upload files to the server. The file is not kept in memory, but sent using a
data stream during transmission, so HTTPRequest is also able to upload large files.name - Parameter nameprovider - HTTPRequest.DataProvider of the data to be uploadedpublic HTTPRequest.Method getMethod()
public boolean isPost()
public void setProgressMonitor(ProgressMonitor monitor, String transfer)
ProgressMonitor for the transmission process. After
doRequest() connected to the server, the transfer string is shown as a
note.
The ProgressMonitor will start at the left corner during start of
transmission, and will end in the right corner of the gauge.
monitor - ProgressMonitor to be used.transfer - Message to be shown after connection.public void setProgressMonitor(ProgressMonitor monitor, String transfer, int min, int max)
ProgressMonitor for the transmission process. After
doRequest() connected to the server, the transfer string is shown as a
note. During transmission, a value between min and max is shown according to the
process of the transmission. After transmission, max will always be the current
value of the ProgressMonitor.
You can use min and max if the HTTPRequest is just one step in a chain of several
steps to be shown in the ProgressMonitor.
Note that the response transfer is not included in the ProgressMonitor.
This is because there is no way to find out the response size from the server
before the request has been entirely sent to it.
monitor - ProgressMonitor to be used.transfer - Message to be shown after connection.min - Minimum value during transmissionmax - Maximum value at the end of transmissionpublic int doRequest() throws IOException, MalformedURLException
ProgressMonitor, it will be used.HttpURLConnectionIOException - IO exceptionMalformedURLException - URL was malformedpublic HttpURLConnection getConnection()
HttpURLConnection object after doRequest() has been
invoked.HttpURLConnection or null if doRequest() was not invoked
yet.public InputStream getResult() throws IOException
InputStream after a
doRequest().InputStream of the server's response.IOException - if the InputStream could not be opened.public String getCharset()
nullpublic Reader getContentReader() throws UnsupportedEncodingException, IOException
Reader to the response body after a doRequest(). The
content charset sent by the server is used. If the given charset is not known, an
exception will be raised. If no charset was given, "ISO-8859-1" is assumed.Reader to the response body.UnsupportedEncodingException - if the encoding given by the server, is not known to this Java
platform.IOException - if an I/O exception occuredCopyright © 2004–2016. All rights reserved.