public class Request extends Object
Modifier and Type | Class and Description |
---|---|
static class |
Request.Method |
Modifier and Type | Method and Description |
---|---|
Response<byte[]> |
asBytes()
Execute the request and expect the result to be convertible to
byte[] . |
Response<org.json.JSONArray> |
asJsonArray()
Execute the request and expect the result to be convertible to
JSONArray . |
Response<org.json.JSONObject> |
asJsonObject()
Execute the request and expect the result to be convertible to
JSONObject . |
Response<InputStream> |
asStream()
Execute the request and expect the result to be convertible to
InputStream . |
Response<String> |
asString()
Execute the request and expect the result to be convertible to
String . |
Response<Void> |
asVoid()
Execute the request and expect no result payload (only status-code and headers).
|
Request |
body(Object body)
Set the payload for the request.
|
Request |
compress()
Enable compression for uploaded data.
|
Request |
connectTimeout(int connectTimeout)
|
Request |
ensureSuccess()
By calling this method, the HTTP status code is checked and a
WebbException is thrown if
the status code is not something like 2xx. |
Request |
followRedirects(boolean auto)
|
String |
getUri()
Get the URI of this request.
|
Request |
header(String name,
Object value)
Set (or overwrite) a HTTP header value.
|
Request |
ifModifiedSince(long ifModifiedSince)
|
Request |
multipleValues()
Turn on a mode where one parameter key can have multiple values.
|
Request |
param(String name,
Iterable<Object> values)
Set (or overwrite) a parameter with multiple values.
|
Request |
param(String name,
Object value)
Set (or overwrite) a parameter.
|
Request |
params(Map<String,Object> valueByName)
Set (or overwrite) many parameters via a map.
|
Request |
readTimeout(int readTimeout)
|
Request |
retry(int retryCount,
boolean waitExponential)
Set the number of retries after the first request failed.
|
Request |
useCaches(boolean useCaches)
See
URLConnection.useCaches
If you don't want your requests delivered from a cache, you don't have to call this method, because false is the default. |
public Request multipleValues()
order.php?fruit=orange&fruit=apple&fruit=banana
param(String, Object)
multiple
times with the same parameter name and this should lead to having multiple values.
If you call param(String, Iterable)
or already provide an Array as value parameter,
you don't have to call this method and it should work as expected.this
for method chaining (fluent API)public Request param(String name, Object value)
application/x-www-form-urlencoded
.
multipleValues()
if you have to deal with parameters carrying multiple values.
name
- the name of the parameter (it's better to use only contain ASCII characters)value
- the value of the parameter; null
will be converted to empty string,
Arrays of Objects are expanded to multiple valued parameters, for all other
objects to toString()
method converts it to Stringthis
for method chaining (fluent API)public Request param(String name, Iterable<Object> values)
application/x-www-form-urlencoded
.
multipleValues()
, but you should not mix
using param(String, Object)
and this method for the same parameter name as this might cause
unexpected behaviour or exceptions.name
- the name of the parameter (it's better to use only contain ASCII characters)values
- the values of the parameter; will be expanded to multiple valued parameters.this
for method chaining (fluent API)public Request params(Map<String,Object> valueByName)
valueByName
- a Map of name-value pairs,null
will be converted to empty string, for all other
objects to toString()
method converts it to Stringthis
for method chaining (fluent API)public String getUri()
public Request header(String name, Object value)
Webb
instance (Webb.setDefaultHeader(String, Object)
) or a global header
(Webb.setGlobalHeader(String, Object)
).
null
or empty String is not allowed for name and value.name
- name of the header (HTTP-headers are not case-sensitive, but if you want to override your own
headers, you have to use identical strings for the name. There are some frequently used header
names as constants in Webb
, see HDR_xxx.value
- the value for the header. Following types are supported, all other types use toString
of the given object:
this
for method chaining (fluent API)public Request body(Object body)
param(String, Object)
has the effect of body
being
ignored without notice. The method can be called more than once: the value will be stored and converted
to bytes later.
null
clears the body
JSONObject
, HTTP header 'Content-Type' will be set to JSON, if not set
JSONArray
, HTTP header 'Content-Type' will be set to JSON, if not set
String
, HTTP header 'Content-Type' will be set to TEXT, if not set;
Text will be converted to UTF-8 bytes.
byte[]
the easiest way for DavidWebb - it's just passed through.
HTTP header 'Content-Type' will be set to BINARY, if not set.
File
, HTTP header 'Content-Type' will be set to BINARY, if not set;
The file gets streamed to the web-server and 'Content-Length' will be set to the number
of bytes of the file. There is absolutely no conversion done. So if you want to upload
e.g. a text-file and convert it to another encoding than stored on disk, you have to do
it by yourself.
InputStream
, HTTP header 'Content-Type' will be set to BINARY, if not set;
Similar to File
. Content-Length cannot be set (which has some drawbacks compared
to knowing the size of the body in advance).body
- the payloadthis
for method chaining (fluent API)public Request compress()
this
for method chaining (fluent API)public Request useCaches(boolean useCaches)
false
is the default.useCaches
- If true
, the protocol is allowed to use caching whenever it can.this
for method chaining (fluent API)public Request ifModifiedSince(long ifModifiedSince)
ifModifiedSince
- A nonzero value gives a time as the number of milliseconds since January 1, 1970, GMT.
The object is fetched only if it has been modified more recently than that time.this
for method chaining (fluent API)public Request connectTimeout(int connectTimeout)
connectTimeout
- sets a specified timeout value, in milliseconds. 0
means infinite timeout.this
for method chaining (fluent API)public Request readTimeout(int readTimeout)
readTimeout
- Sets the read timeout to a specified timeout, in milliseconds.
0
means infinite timeout.this
for method chaining (fluent API)public Request followRedirects(boolean auto)
Webb.setFollowRedirects(boolean)
.auto
- true
to automatically follow redirects (HTTP status code 3xx).
Default value comes from HttpURLConnection and should be true
.this
for method chaining (fluent API)public Request ensureSuccess()
WebbException
is thrown if
the status code is not something like 2xx.ifModifiedSince(long)
, an exception will also be
thrown in the positive case of 304 Not Modified
.this
for method chaining (fluent API)public Request retry(int retryCount, boolean waitExponential)
Thread.sleep(long)
between
the retries. If the thread is interrupted, there will be an `InterruptedException`
in the thrown `WebbException`. You can check this with Throwable.getCause()
.
The `interrupted` flag will be set to true in this case.retryCount
- This parameter holds the number of retries that will be made AFTER the
initial send in the event of a error. If an error occurs on the last
attempt an exception will be raised.waitExponential
- sleep during retry attempts (exponential backoff).
For retry-counts more than 3, true is mandatory.this
for method chaining (fluent API)public Response<String> asString()
String
.Response
object carrying the payload from the server as String
public Response<org.json.JSONObject> asJsonObject()
JSONObject
.Response
object carrying the payload from the server as JSONObject
public Response<org.json.JSONArray> asJsonArray()
JSONArray
.Response
object carrying the payload from the server as JSONArray
public Response<byte[]> asBytes()
byte[]
.Response
object carrying the payload from the server as byte[]
public Response<InputStream> asStream()
InputStream
.Response
object carrying the payload from the server as InputStream
Copyright © 2016. All rights reserved.