|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.univocity.api.net.HttpMessage
com.univocity.api.net.HttpRequest
public class HttpRequest
A reusable, cloneable HTTP request configuration, with support for parameterization of the URL.
Example of a URL with 2 parameters (QUERY and PERIOD): "https://www.google.com/?q={QUERY}#q={QUERY}&tbs=qdr:{PERIOD}"
Use setUrlParameter(String, Object) to set the values of any parameters so that getUrl()
generates the final URL.
RequestMethod,
HttpResponse,
HttpResponseReader,
UrlReaderProvider| Field Summary |
|---|
| Fields inherited from class com.univocity.api.net.HttpMessage |
|---|
cookies, headers, requestMethod |
| Constructor Summary | |
|---|---|
protected |
HttpRequest(String url,
RateLimiter rateLimiter)
Creates a new request for a given request URL |
| Method Summary | |
|---|---|
void |
addCookies(Map<String,String> cookies)
Adds the given cookies to the Cookie HTTP header. |
void |
addDataParameter(DataParameter dataParameter)
Adds a data parameter to the body of RequestMethod.POST requests as a plain String |
void |
addDataParameter(String paramName,
Object value)
Adds a parameter to the body of RequestMethod.POST requests as a plain String |
void |
addDataParameter(String key,
String filename,
InputStream inputStream)
Add an input stream as a request data parameter. |
void |
addDataParameter(String key,
String filename,
InputStream inputStream,
String contentType)
Add an input stream as a request data parameter. |
void |
addDataParameter(String key,
String filename,
ResourceProvider<InputStream> inputStreamProvider)
Add an input stream as a request data parameter. |
void |
addDataParameter(String key,
String filename,
ResourceProvider<InputStream> inputStreamProvider,
String contentType)
Add an input stream as a request data parameter. |
void |
addDataParameters(Collection<DataParameter> data)
Adds multiple data parameters to this HTTP request |
void |
addDataParameters(Map<String,?> data)
Adds multiple data parameters to the body of this request |
void |
addDataParameters(Map<String,String[]> params,
String... keys)
Adds multiple parameters to the body of RequestMethod.POST requests as a plain Strings. |
void |
addHeader(String header,
String value)
Associates a value to a header. |
void |
addHeader(String header,
String value,
boolean encode)
Associates a value to a header. |
void |
addHeaders(Map<String,String> headers)
Adds the given values to the existing list of headers. |
void |
addHeaders(Map<String,String> headers,
boolean encode)
Adds the given values to the existing list of headers.. |
void |
clearDataParameters()
Removes all data parameters set on the body of this request. |
void |
clearUrlParameters()
Clears all values assigned to all parameters of the URL provided in the constructor of this class. |
HttpRequest |
clone()
Clones this request and all its configurations. |
boolean |
equals(Object o)
|
int |
getBodySizeLimit()
Returns the size limit when reading the HTTP response body, effectively truncating the response if the number of characters read exceed the given limit. |
Charset |
getCharset()
Returns the charset to be used when reading the response resulting from this HTTP request. |
String |
getCharsetName()
Returns the name of the charset to be used when reading the response resulting from this HTTP request. |
String |
getCharsetName(String defaultIfNull)
Returns the name of the charset to be used when reading the response resulting from this HTTP request. |
List<DataParameter> |
getData()
Returns the data parameters sent by on the body of this request if it is a RequestMethod.POST request. |
List<DataParameter> |
getDataParameter(String paramName)
Returns the values associated with a parameter of the body a RequestMethod.POST request |
DataParameter |
getFirstDataParameter(String key)
Returns the first DataParameter details associated with a given data parameter name. |
boolean |
getFollowRedirects()
Returns a flag indicating if the request should follow redirects. |
Charset |
getPostDataCharset()
Returns the POST data character set for x-www-form-urlencoded POST data |
String |
getPostDataCharsetName()
Returns the name of the POST data character set for x-www-form-urlencoded POST data |
Proxy |
getProxy()
Returns the the proxy this request should use to connect to |
char[] |
getProxyPassword()
Returns the password to be used when connecting with a proxy |
String |
getProxyUser()
Returns the username to be used when authenticating with a proxy |
RateLimiter |
getRateLimiter()
Returns the RateLimiter associated with this request, if any. |
String |
getRequestBody()
Returns the plain request body to be sent by this request. |
SSLSocketFactory |
getSslSocketFactory()
Get the current custom SSL socket factory, if any. |
int |
getTimeout()
Returns the time limit (in milliseconds) for an initial connection to be established from this request. |
String |
getUrl()
Returns the request URL, with any parameters replaced by their values (if any) |
Object |
getUrlParameter(String parameterName)
Returns the value of a given parameter of the URL provided in the constructor of this class. |
String |
getUrlParameter(String parameterName,
boolean decode)
Returns the value of a given parameter of the URL provided in the constructor of this class. |
Set<String> |
getUrlParameters()
Returns the names of all available URL parameters in an unmodifiable set. |
int |
hashCode()
|
boolean |
isIgnoreHttpErrors()
Returns whether this HTTP request to ignore HTTP errors returned by the remote server and process the response anyway. |
String |
printDetails()
Returns a String with all relevant details of this HTTP request, including cookies, headers, data |
void |
removeCookie(String name)
Removes a given cookie and its value from the Cookie header of this HTTP request |
void |
removeDataParameter(String paramName)
Removes a given data parameter from the body of a RequestMethod.POST request |
void |
removeHeader(String name)
Removes a given header and all values associated with it from this HTTP request |
void |
setBodySizeLimit(int limit)
Defines a size limit when reading the HTTP response body, effectively truncating the response if the number of characters read exceed the given limit. |
void |
setCharset(Charset charset)
Defines a charset to be used when reading the response resulting from this HTTP request. |
void |
setCharset(String charset)
Defines a charset to be used when reading the response resulting from this HTTP request. |
void |
setCookie(String cookie,
String value)
Sets a cookie to be added to the Cookie HTTP header. |
void |
setCookies(Map<String,String> cookies)
Replaces the cookies to be added to the Cookie HTTP header. |
void |
setDataParameter(DataParameter dataParameter)
Adds/replaces a parameter of the body of RequestMethod.POST requests as a plain String |
void |
setDataParameter(String paramName,
Object value)
Replaces/adds a data parameter to the body of this request |
void |
setDataParameters(Map<String,String[]> params,
String... keys)
Sets multiple parameters to the body of RequestMethod.POST requests as a plain Strings. |
void |
setFollowRedirects(boolean followRedirects)
Configures the request to follow redirects. |
void |
setHeader(String header,
String value)
Defines a header and its value. |
void |
setHeader(String header,
String value,
boolean encode)
Defines a header and its value. |
void |
setHeaders(Map<String,String> headers)
Replaces any previous headers with the given values. |
void |
setHeaders(Map<String,String> headers,
boolean encode)
Replaces any previous headers with the given values. |
void |
setIgnoreHttpErrors(boolean ignoreHttpErrors)
Configures the this HTTP request to ignore HTTP errors returned by the remote server and process the response anyway. |
void |
setKeepAliveEnabled(boolean enableKeepAlive)
reused for further requests, or to close if the connection should be discarded and closed as soon as the
Sets the Connection request header to explicitly to keep-alive if the connection is meant to be
response is processed. |
void |
setPostDataCharset(Charset postDataCharset)
Sets the POST data character set for x-www-form-urlencoded POST data |
void |
setPostDataCharset(String charsetName)
Sets the POST data character set for x-www-form-urlencoded POST data |
void |
setProxy(Proxy.Type proxyType,
String host,
int port,
String user,
char[] password)
Configures this request to connect through a proxy. |
void |
setProxy(Proxy proxy)
Configures this request to connect through a proxy. |
void |
setProxy(Proxy proxy,
String user)
Configures this request to connect through a proxy with authentication. |
void |
setProxy(Proxy proxy,
String user,
char[] password)
Configures this request to connect through a proxy with authentication. |
void |
setProxy(String host,
int port)
Configures this request to connect through a proxy. |
void |
setProxy(String host,
int port,
String user)
Configures this request to connect through a proxy. |
void |
setProxy(String host,
int port,
String user,
char[] password)
Configures this request to connect through a proxy. |
void |
setRateLimiter(RateLimiter rateLimiter)
Associates a RateLimiter with this request to prevent multiple simultaneous requests. |
void |
setReferrer(String referrer)
Defines a Referer request header that identifies the address of the web-page
that linked to the resource being requested. |
void |
setRequestBody(String body)
Defines a plain request body to be sent by this request. |
void |
setRequestMethod(RequestMethod httpMethodType)
Sets the RequestMethod to be used by this request. |
void |
setSslSocketFactory(SSLSocketFactory sslSocketFactory)
Set a custom SSL socket factory. |
void |
setTimeout(int timeout)
Defines a time limit (in milliseconds) for an initial connection to be established from this request. |
void |
setUrl(String url)
Modifies the URL this HTTP request. |
void |
setUrlParameter(String parameterName,
Object parameterValue)
Assigns a value to a given parameter of the URL provided in the constructor of this class. |
void |
setUrlParameter(String parameterName,
Object parameterValue,
boolean encode)
Assigns a value to a given parameter of the URL provided in the constructor of this class. |
void |
setUserAgent(String userAgent)
Defines a User-Agent request header, which identifies the user agent originating the request. |
String |
toString()
|
| Methods inherited from class com.univocity.api.net.HttpMessage |
|---|
getCookies, getCookieValue, getHeaders, getHeaderValue, getHeaderValues, getMultiHeaders, getReferrer, getRequestMethod, getUserAgent, hasCookie, hasHeader, hasHeaderWithValue, isKeepAliveEnabled |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
protected HttpRequest(String url,
RateLimiter rateLimiter)
url - the request URLrateLimiter - a RateLimiter to prevent the execution of excessive simultaneous requests.| Method Detail |
|---|
public final RateLimiter getRateLimiter()
RateLimiter associated with this request, if any.
public final void setRateLimiter(RateLimiter rateLimiter)
RateLimiter with this request to prevent multiple simultaneous requests.
rateLimiter - the rate limiter to be used when executing this request.public final void setUrl(String url)
url - the new request URL.public final int getBodySizeLimit()
public final void setBodySizeLimit(int limit)
limit - the maximum number of characters to read from the responsepublic final String getRequestBody()
String.public final void setRequestBody(String body)
body - the request body String.public final Charset getPostDataCharset()
x-www-form-urlencoded POST data
public final String getPostDataCharsetName()
x-www-form-urlencoded POST data
public final void setPostDataCharset(String charsetName)
x-www-form-urlencoded POST data
charsetName - name of the character setpublic final void setPostDataCharset(Charset postDataCharset)
x-www-form-urlencoded POST data
postDataCharset - the character set to usepublic final void setKeepAliveEnabled(boolean enableKeepAlive)
close if the connection should be discarded and closed as soon as the
Sets the Connection request header to explicitly to keep-alive if the connection is meant to be
response is processed.
enableKeepAlive - flag indicating whether or not the HTTP connection should be persistent (i.e kept alive).public final void setUserAgent(String userAgent)
User-Agent request header, which identifies the user agent originating the request.
userAgent - the new User-Agent valuepublic final void setReferrer(String referrer)
Referer request header that identifies the address of the web-page
that linked to the resource being requested.
referrer - the new Referer value
public final void addHeader(String header,
String value)
header - the header namevalue - the header value
public final void addHeader(String header,
String value,
boolean encode)
header - the header namevalue - the header valueencode - flag indicating whether to encode the given value
public final void setHeader(String header,
String value)
header - the header namevalue - the header value
public final void setHeader(String header,
String value,
boolean encode)
header - the header namevalue - the new header valueencode - flag indicating whether the value should be encodedpublic final void setHeaders(Map<String,String> headers)
headers - a map of headers names and their values. Previous values will be discardedpublic final void addHeaders(Map<String,String> headers)
headers - a map of header names and their values.
public final void setHeaders(Map<String,String> headers,
boolean encode)
headers - a map of headers names and their values. Previous values will be discardedencode - flag indicating whether values should be encoded
public final void addHeaders(Map<String,String> headers,
boolean encode)
headers - a map of header names and their values.encode - flag indicating whether values should be encoded
public final void setCookie(String cookie,
String value)
Cookie HTTP header.
All cookies are sent in this header in the format of semicolon-separated name-value pairs.
cookie - the cookie namevalue - the cookie valuepublic final void setCookies(Map<String,String> cookies)
Cookie HTTP header.
All cookies are sent in this header in the format of semicolon-separated name-value pairs.
cookies - a map of cookie names and their values. Previous values will be discardedpublic final void addCookies(Map<String,String> cookies)
Cookie HTTP header.
All cookies are sent in this header in the format of semicolon-separated name-value pairs.
cookies - a map of cookie names and their values.public final void removeCookie(String name)
Cookie header of this HTTP request
name - the name of the cookie to be removed.public final void setTimeout(int timeout)
timeout - the time limit to wait until a connection is established.public final void setRequestMethod(RequestMethod httpMethodType)
RequestMethod to be used by this request.
The method type identifies an action to be performed on the identified (remote) resource.
Defaults to RequestMethod.GET
httpMethodType - the HTTP method to usepublic final String getUrl()
public final void setUrlParameter(String parameterName,
Object parameterValue)
getUrl() will return the updated target URL of this request. Parameters without values
won't be replaced in the URL.
parameterName - name of the parameter enclosed within { and } in the URLparameterValue - value of the given parameter, to replace the parameter name in the URL
IllegalArgumentException - if the parameter name does not existpublic final Object getUrlParameter(String parameterName)
getUrl() will return the updated target URL of this request. Parameters without values
won't be replaced in the URL.
parameterName - name of the parameter enclosed within { and } in the URL
IllegalArgumentException - if the parameter name does not existpublic final Set<String> getUrlParameters()
public final void setUrlParameter(String parameterName,
Object parameterValue,
boolean encode)
getUrl() will return the updated target URL of this request. Parameters without values
won't be replaced in the URL.
parameterName - name of the parameter enclosed within { and } in the URLparameterValue - value of the given parameter, to replace the parameter name in the URLencode - flag indicating whether to encode the given parameter value.
If the value is already encoded, it won't be encoded again.
IllegalArgumentException - if the parameter name does not existpublic final String getCharsetName(String defaultIfNull)
Content-Type header of the HTTP response.
defaultIfNull - a default charset name to return if the result of getCharsetName() is null
public final String getUrlParameter(String parameterName,
boolean decode)
getUrl() will return the updated target URL of this request. Parameters without values
won't be replaced in the URL.
parameterName - name of the parameter enclosed within { and } in the URLdecode - flag indicating whether to decode the value associated with the given parameter name.
IllegalArgumentException - if the parameter name does not existpublic final void clearUrlParameters()
getUrl() after this method will return the original URL with
parameters provided in the constructor.
public final int getTimeout()
public final boolean getFollowRedirects()
HttpResponse.getRedirectionUrl()
should return the redirection URL if this request was redirected.
Defaults to true.
public final void setFollowRedirects(boolean followRedirects)
HttpResponse.getRedirectionUrl() should return
the redirection URL if this request was redirected.
Defaults to true.
followRedirects - flag indicating whether or not to follow redirects.public final void addDataParameter(DataParameter dataParameter)
RequestMethod.POST requests as a plain String
dataParameter - the data parameterpublic final void removeDataParameter(String paramName)
RequestMethod.POST request
paramName - the parameter namepublic final List<DataParameter> getDataParameter(String paramName)
RequestMethod.POST request
paramName - the parameter name
public final void setDataParameter(DataParameter dataParameter)
RequestMethod.POST requests as a plain String
dataParameter - the new/updated data parameter
public final void setDataParameters(Map<String,String[]> params,
String... keys)
RequestMethod.POST requests as a plain Strings.
Multiple values can be associated with a parameter name. Any previous parameters will be removed.
null or empty lists of values will cause an empty String to be associated with the parameter name.
params - the parameters and values associated with them. Multiple values can be associated with each
parameter name.keys - the specific keys to read from the given parameter map. Parameters will be added in the order
defined by the key sequence. If the map doesn't contain a given key, a parameter named after
the key will be created, assigning an empty String value to it.
If no keys are provided, all elements of the map will be used.
public final void addDataParameters(Map<String,String[]> params,
String... keys)
RequestMethod.POST requests as a plain Strings.
Multiple values can be associated with a parameter name.
null or empty lists of values will cause an empty String to be associated with the parameter name.
params - the parameters and values associated with them. Multiple values can be associated with each
parameter name.keys - the specific keys to read from the given parameter map. Parameters will be added in the order
defined by the key sequence. If the map doesn't contain a given key, a parameter named after
the key will be created, assigning an empty String value to it.
If no keys are provided, all elements of the map will be used.
public final void addDataParameter(String paramName,
Object value)
RequestMethod.POST requests as a plain String
paramName - the parameter namevalue - the parameter value
public final void setDataParameter(String paramName,
Object value)
paramName - the parameter namevalue - the parameter valuepublic final List<DataParameter> getData()
RequestMethod.POST request.
public final void removeHeader(String name)
name - the name of the header to be removed
public void addDataParameter(String key,
String filename,
InputStream inputStream,
String contentType)
key - data key (form item name)filename - the name of the file to present to the remove server. Typically just the name, not path,
component.inputStream - the input stream to upload.contentType - the Content Type (aka mimetype) to specify for this file.
You must close the InputStream in a finally block.
public void addDataParameter(String key,
String filename,
ResourceProvider<InputStream> inputStreamProvider,
String contentType)
key - data key (form item name)filename - the name of the file to present to the remove server. Typically just the name, not path,
component.inputStreamProvider - the input stream provider to upload.contentType - the Content Type (aka mimetype) to specify for this file.
You must close the InputStream in a finally block.public void addDataParameters(Map<String,?> data)
data - a map of data parameter names and their corresponding values
public void addDataParameter(String key,
String filename,
InputStream inputStream)
key - data key (form item name)filename - the name of the file to present to the remove server. Typically just the name, not path,
component.inputStream - the input stream to upload, that you probably obtained from a FileInputStream.
public void addDataParameter(String key,
String filename,
ResourceProvider<InputStream> inputStreamProvider)
key - data key (form item name)filename - the name of the file to present to the remove server. Typically just the name, not path,
component.inputStreamProvider - a callback provider that produces the input stream of the content to upload.public DataParameter getFirstDataParameter(String key)
DataParameter details associated with a given data parameter name.
key - the data parameter name
DataParameter instance associated with the given name, or null if none.public final void addDataParameters(Collection<DataParameter> data)
data - the data parameters to addpublic final void clearDataParameters()
public final void setProxy(Proxy proxy)
proxy - the proxy configuration.
public final void setProxy(Proxy proxy,
String user)
proxy - the proxy configuration.user - the proxy user.
public final void setProxy(Proxy proxy,
String user,
char[] password)
proxy - the proxy configuration.user - the proxy user.password - the proxy password.
public final void setProxy(String host,
int port)
host - the proxy host.port - the proxy port.
public final void setProxy(String host,
int port,
String user)
host - the proxy host.port - the proxy port.user - the proxy user.
public final void setProxy(String host,
int port,
String user,
char[] password)
host - the proxy host.port - the proxy port.user - the proxy user.password - the proxy password
public final void setProxy(Proxy.Type proxyType,
String host,
int port,
String user,
char[] password)
proxyType - the type of proxy.host - the proxy host.port - the proxy port.user - the proxy user.password - the proxy password (note the char array is copied)public final String getProxyUser()
public final char[] getProxyPassword()
public final Proxy getProxy()
public final void setCharset(String charset)
Content-Type header of the HTTP response.
charset - the charset namepublic final void setCharset(Charset charset)
Content-Type header of the HTTP response.
charset - the charsetpublic final Charset getCharset()
Content-Type header of the HTTP response.
public final String getCharsetName()
Content-Type header of the HTTP response.
public final boolean isIgnoreHttpErrors()
false.
public final void setIgnoreHttpErrors(boolean ignoreHttpErrors)
false.
ignoreHttpErrors - flag indicating whether or not HTTP response code errors should be ignoredpublic final SSLSocketFactory getSslSocketFactory()
null otherwisepublic final void setSslSocketFactory(SSLSocketFactory sslSocketFactory)
sslSocketFactory - SSL socket factorypublic final HttpRequest clone()
clone in class Objectpublic final String printDetails()
String with all relevant details of this HTTP request, including cookies, headers, data
public final String toString()
toString in class Objectpublic final boolean equals(Object o)
equals in class Objectpublic final int hashCode()
hashCode in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||