public final class HttpClient extends 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 HttpURLConnection |
connection |
private int |
connectTimeout |
private Map<String,String> |
headers |
private long |
ifModifiedSince |
private int |
maxRedirects |
private int |
readTimeout |
private String |
reasonForRequest |
private byte[] |
requestBody |
private String |
requestMethod |
private HttpClient.Response |
response |
private URL |
url |
private boolean |
useCache |
Modifier | Constructor and Description |
---|---|
private |
HttpClient(URL url,
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(URL url)
Creates a new instance for the given URL and a
GET request |
static HttpClient |
create(URL url,
String requestMethod)
Creates a new instance for the given URL and a
GET request |
void |
disconnect() |
private static void |
disconnect(HttpURLConnection connection) |
String |
getRequestHeader(String header)
Returns the set value for the given
header . |
String |
getRequestMethod()
Returns the request method set for this connection.
|
HttpClient.Response |
getResponse()
Returns the HTTP response which is set only after calling
connect() . |
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(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(String key,
String value)
Sets an arbitrary HTTP header.
|
HttpClient |
setHeaders(Map<String,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(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 final String requestMethod
private int connectTimeout
private int readTimeout
private byte[] requestBody
private long ifModifiedSince
private int maxRedirects
private boolean useCache
private String reasonForRequest
private transient HttpURLConnection connection
private transient HttpClient.Response response
private HttpClient(URL url, String requestMethod)
public HttpClient.Response connect() throws IOException
IOException
- if any I/O error occurspublic HttpClient.Response connect(ProgressMonitor progressMonitor) throws IOException
progressMonitor
- progress monitorIOException
- 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(URL url)
GET
requesturl
- the URLpublic static HttpClient create(URL url, String requestMethod)
GET
requesturl
- the URLrequestMethod
- the HTTP request method to perform when callingpublic URL getURL()
create(URL)
,
create(URL, String)
public String getRequestMethod()
create(URL, String)
public String getRequestHeader(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(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(String key, String value)
key
- header namevalue
- header valuethis
public HttpClient setHeaders(Map<String,String> headers)
headers
- HTTP headersthis
public HttpClient setReasonForRequest(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(HttpURLConnection connection)