public final class HttpClient extends java.lang.Object
HttpURLConnection.| Modifier and Type | Class and Description |
|---|---|
static class |
HttpClient.Response
A wrapper for the HTTP response.
|
| Modifier and Type | Field and Description |
|---|---|
private java.net.HttpURLConnection |
connection |
private int |
connectTimeout |
private boolean |
finishOnCloseOutput |
private java.util.Map<java.lang.String,java.lang.String> |
headers |
private long |
ifModifiedSince |
private int |
maxRedirects |
private java.lang.String |
outputMessage |
private int |
readTimeout |
private java.lang.String |
reasonForRequest |
private byte[] |
requestBody |
private java.lang.String |
requestMethod |
private HttpClient.Response |
response |
private static java.util.regex.Pattern |
TOMCAT_ERR_MESSAGE |
private java.net.URL |
url |
private boolean |
useCache |
| Modifier | Constructor and Description |
|---|---|
private |
HttpClient(java.net.URL url,
java.lang.String requestMethod) |
| Modifier and Type | Method and Description |
|---|---|
HttpClient.Response |
connect()
Opens the HTTP connection.
|
HttpClient.Response |
connect(ProgressMonitor progressMonitor)
Opens the HTTP connection.
|
static HttpClient |
create(java.net.URL url)
Creates a new instance for the given URL and a
GET request |
static HttpClient |
create(java.net.URL url,
java.lang.String requestMethod)
Creates a new instance for the given URL and a
GET request |
void |
disconnect() |
private static void |
disconnect(java.net.HttpURLConnection connection) |
java.lang.String |
getRequestHeader(java.lang.String header)
Returns the set value for the given
header. |
java.lang.String |
getRequestMethod()
Returns the request method set for this connection.
|
HttpClient.Response |
getResponse()
Returns the HTTP response which is set only after calling
connect(). |
static java.util.regex.Matcher |
getTomcatErrorMatcher(java.lang.String data)
Returns a
Matcher against predefined Tomcat error messages. |
java.net.URL |
getURL()
Returns the URL set for this connection.
|
private static boolean |
isRedirect(int statusCode) |
HttpClient |
keepAlive(boolean keepAlive)
Sets whether not to set header
Connection=close |
HttpClient |
setAccept(java.lang.String accept)
Sets the
Accept header. |
HttpClient |
setConnectTimeout(int connectTimeout)
Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced
by this URLConnection.
|
HttpClient |
setFinishOnCloseOutput(boolean finishOnCloseOutput)
Sets whether the progress monitor task will be finished when the output stream is closed.
|
HttpClient |
setHeader(java.lang.String key,
java.lang.String value)
Sets an arbitrary HTTP header.
|
HttpClient |
setHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
Sets arbitrary HTTP headers.
|
HttpClient |
setIfModifiedSince(long ifModifiedSince)
Sets the
If-Modified-Since header. |
HttpClient |
setMaxRedirects(int maxRedirects)
Sets the maximum number of redirections to follow.
|
HttpClient |
setOutputMessage(java.lang.String outputMessage)
Sets the output message to be displayed in progress monitor for
PUT, POST and DELETE methods. |
HttpClient |
setReadTimeout(int readTimeout)
Sets the read timeout to a specified timeout, in milliseconds.
|
HttpClient |
setReasonForRequest(java.lang.String reasonForRequest)
Sets a reason to show on console.
|
HttpClient |
setRequestBody(byte[] requestBody)
Sets the request body for
PUT/POST requests. |
HttpClient |
useCache(boolean useCache)
Sets whether not to set header
Cache-Control=no-cache |
private java.net.URL url
private final java.lang.String requestMethod
private int connectTimeout
private int readTimeout
private byte[] requestBody
private long ifModifiedSince
private final java.util.Map<java.lang.String,java.lang.String> headers
private int maxRedirects
private boolean useCache
private java.lang.String reasonForRequest
private java.lang.String outputMessage
private java.net.HttpURLConnection connection
private HttpClient.Response response
private boolean finishOnCloseOutput
private static final java.util.regex.Pattern TOMCAT_ERR_MESSAGE
private HttpClient(java.net.URL url, java.lang.String requestMethod)
public HttpClient.Response connect() throws java.io.IOException
java.io.IOException - if any I/O error occurspublic HttpClient.Response connect(ProgressMonitor progressMonitor) throws java.io.IOException
progressMonitor - progress monitorjava.io.IOException - if any I/O error occurspublic HttpClient.Response getResponse()
connect().
Calling this method again, returns the identical object (unless another connect() is performed).public static HttpClient create(java.net.URL url)
GET requesturl - the URLpublic static HttpClient create(java.net.URL url, java.lang.String requestMethod)
GET requesturl - the URLrequestMethod - the HTTP request method to perform when callingpublic java.net.URL getURL()
create(URL),
create(URL, String)public java.lang.String getRequestMethod()
create(URL, String)public java.lang.String getRequestHeader(java.lang.String header)
header.header - HTTP header namepublic HttpClient useCache(boolean useCache)
Cache-Control=no-cacheuseCache - whether not to set header Cache-Control=no-cachethisURLConnection.setUseCaches(boolean)public HttpClient keepAlive(boolean keepAlive)
Connection=close
This might fix #7640, see here.
keepAlive - whether not to set header Connection=closethispublic HttpClient setConnectTimeout(int connectTimeout)
SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.connectTimeout - an int that specifies the connect timeout value in millisecondsthisURLConnection.setConnectTimeout(int)public HttpClient setReadTimeout(int readTimeout)
SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.readTimeout - an int that specifies the read timeout value in millisecondsthisURLConnection.setReadTimeout(int)public HttpClient setAccept(java.lang.String accept)
Accept header.accept - header valuethispublic HttpClient setRequestBody(byte[] requestBody)
PUT/POST requests.requestBody - request bodythispublic HttpClient setIfModifiedSince(long ifModifiedSince)
If-Modified-Since header.ifModifiedSince - header valuethispublic HttpClient setMaxRedirects(int maxRedirects)
maxRedirects to -1 in order to ignore redirects, i.e.,
to not throw an IOException in connect().maxRedirects - header valuethispublic HttpClient setHeader(java.lang.String key, java.lang.String value)
key - header namevalue - header valuethispublic HttpClient setHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
headers - HTTP headersthispublic HttpClient setReasonForRequest(java.lang.String reasonForRequest)
null if no reason is given.reasonForRequest - Reason to showthispublic HttpClient setOutputMessage(java.lang.String outputMessage)
PUT, POST and DELETE methods.
Defaults to "Uploading data ..." (translated). Has no effect for GET or any other method.outputMessage - message to be displayed in progress monitorthispublic HttpClient setFinishOnCloseOutput(boolean finishOnCloseOutput)
true by default.finishOnCloseOutput - whether the progress monitor task will be finished when the output stream is closedthisprivate static boolean isRedirect(int statusCode)
public void disconnect()
HttpURLConnection.disconnect()private static void disconnect(java.net.HttpURLConnection connection)
public static java.util.regex.Matcher getTomcatErrorMatcher(java.lang.String data)
Matcher against predefined Tomcat error messages.
If it matches, error message can be extracted from group(1).data - HTML contents to checkMatcher against predefined Tomcat error messages