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 java.util.Map<java.lang.String,java.lang.String> |
headers |
private long |
ifModifiedSince |
private int |
maxRedirects |
private int |
readTimeout |
private java.lang.String |
reasonForRequest |
private byte[] |
requestBody |
private java.lang.String |
requestMethod |
private HttpClient.Response |
response |
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() . |
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 |
setFixedLengthStreamingMode(long contentLength)
Deprecated.
Submitting data via POST, PUT, DELETE automatically sets this property on the connection
|
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 |
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 transient java.net.HttpURLConnection connection
private transient HttpClient.Response response
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-cache
useCache
- whether not to set header Cache-Control=no-cache
this
URLConnection.setUseCaches(boolean)
public HttpClient keepAlive(boolean keepAlive)
Connection=close
This might fix #7640, see here.
keepAlive
- whether not to set header Connection=close
this
public 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 millisecondsthis
URLConnection.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 millisecondsthis
URLConnection.setReadTimeout(int)
@Deprecated public HttpClient setFixedLengthStreamingMode(long contentLength)
An exception will be thrown if the application attempts to write more data than the indicated content-length, or if the application closes the OutputStream before writing the indicated amount.
When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required. This exception can be queried for the details of the error.
contentLength
- The number of bytes which will be written to the OutputStreamthis
HttpURLConnection.setFixedLengthStreamingMode(long)
public HttpClient setAccept(java.lang.String accept)
Accept
header.accept
- header valuethis
public HttpClient setRequestBody(byte[] requestBody)
PUT
/POST
requests.requestBody
- request bodythis
public HttpClient setIfModifiedSince(long ifModifiedSince)
If-Modified-Since
header.ifModifiedSince
- header valuethis
public HttpClient setMaxRedirects(int maxRedirects)
maxRedirects
to -1
in order to ignore redirects, i.e.,
to not throw an IOException
in connect()
.maxRedirects
- header valuethis
public HttpClient setHeader(java.lang.String key, java.lang.String value)
key
- header namevalue
- header valuethis
public HttpClient setHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
headers
- HTTP headersthis
public HttpClient setReasonForRequest(java.lang.String reasonForRequest)
null
if no reason is given.reasonForRequest
- Reason to showthis
private static boolean isRedirect(int statusCode)
public void disconnect()
HttpURLConnection.disconnect()
private static void disconnect(java.net.HttpURLConnection connection)