Class HttpRequest
HTTP request.
-
Nested Class Summary
Nested classes/interfaces inherited from class HttpBase
HttpBase.Defaults -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intprotected booleanprotected Stringprotected Stringprotected HttpConnectionprotected HttpConnectionProviderprotected intprotected Stringprotected Stringprotected intprotected Stringprotected HttpMultiMap<String> protected Stringprotected intprivate booleanprivate booleanFields inherited from class HttpBase
body, capitalizeHeaderKeys, charset, form, formEncoding, HEADER_ACCEPT, HEADER_ACCEPT_ENCODING, HEADER_AUTHORIZATION, HEADER_CLOSE, HEADER_CONNECTION, HEADER_CONTENT_ENCODING, HEADER_CONTENT_LENGTH, HEADER_CONTENT_TYPE, HEADER_ETAG, HEADER_HOST, HEADER_KEEP_ALIVE, HEADER_USER_AGENT, headers, HTTP_1_0, HTTP_1_1, httpProgressListener, httpVersion, mediaType, multipart -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void_reset()Resets the request by resetting all additional values added during the sending.private HttpResponse_send()Accepts JSON content type.basicAuthentication(String username, String password) Enables basic authentication by adding required header.protected Bufferbuffer(boolean fullRequest) Prepares the request buffer.Clears all query parameters.static HttpRequestBuilds a CONNECT request.ReturnsHttpConnectionthat is going to be used for sending this request.Returns http connection provider that was used for creating current http connection.intReturns socket connection timeout.connectionTimeout(int milliseconds) Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets).Specifies JSON content type.Sets cookies to the request.static HttpRequestGeneric request builder, usually used when method is a variable.static HttpRequestBuilds a DELETE request.followRedirects(boolean followRedirects) Defines if redirects responses should be followed.fragment()Returns HTTP fragment, if specified; otherwise returnsnull.static HttpRequestBuilds a GET request.static HttpRequestBuilds a HEAD request.host()Returns request host name.Sets request host name.hostUrl()Returns just host url, without path and query.protected voidPrepares request on creation.booleanReturnstrueif redirects are followed.keepAlive(HttpResponse httpResponse, boolean doContinue) Continues using the same keep-alive connection.intReturns max number of redirects, used whenfollowRedirectsis enabled.maxRedirects(int maxRedirects) Sets the max number of redirects, used whenfollowRedirectsis enabled.method()Returns request method.Specifies request method.method(jodd.net.HttpMethod httpMethod) monitor(HttpProgressListener httpProgressListener) Registerslistenerthat will monitor upload progress.multipart(boolean multipart) Forces multipart requests.open()Opens a newconnectionusing either provided ordefaultconnection provider.open(HttpConnection httpConnection) Assignees providedHttpConnectionfor communication.open(HttpConnectionProvider httpConnectionProvider) Opens a newconnectionusing givenHttpConnectionProvider.static HttpRequestBuilds an OPTIONS request.static HttpRequestBuilds a PATCH request.path()Returns request path, without the query.Sets request path.intport()Returns request port number.port(int port) Sets request port number.static HttpRequestBuilds a POST request.protocol()Returns used protocol.Defines protocol.static HttpRequestBuilds a PUT request.query()Returns backend map of query parameters.Adds many query parameters at once.Adds query parameter.Adds all parameters from the provided map.queryEncoding(String encoding) Defines encoding for query parameters.queryRemove(String name) Removes query parameters for given name.Generates query string.queryString(String queryString) queryString(String queryString, boolean decode) Sets query from provided query string.static HttpRequestreadFrom(InputStream in) Parses input stream and creates newHttpRequestobject.static HttpRequestreadFrom(InputStream in, String encoding) send()Opens connectionif not already open, sends request, reads response and closes the request.voidsendAndReceive(Consumer<HttpResponse> responseHandler) Syntax sugar.<R> RsendAndReceive(Function<HttpResponse, R> responseHandler) Syntax sugar.Sends http request asynchronously using common fork-join pool.Sets the destination (method, host, port...Sets 'Host' header from current host and port.inttimeout()Returns read timeout (SO_TIMEOUT) in milliseconds.timeout(int milliseconds) Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets).tokenAuthentication(String token) Enables token-based authentication.static HttpRequestBuilds a TRACE request.booleanReturns a flag if to trusts all certificates.trustAllCerts(boolean trust) Trusts all certificates, use with caution.url()Returns full URL path.booleanReturns a flag if to verify https hosts.verifyHttpsHost(boolean verifyHttpsHost) Verifies HTTPS hosts.withConnectionProvider(HttpConnectionProvider httpConnectionProvider) Uses custom connection provider whenopeningthe connection.Methods inherited from class HttpBase
_header, _headerRaw, _this, accept, accept, acceptEncoding, acceptEncoding, body, body, bodyBytes, bodyRaw, bodyText, bodyText, bodyText, bodyText, capitalizeHeaderKeys, capitalizeHeaderKeys, charset, charset, connectionKeepAlive, contentEncoding, contentLength, contentLength, contentType, contentType, contentType, cookies, form, form, form, form, formBuffer, formEncoding, formOverwrite, header, header, header, header, header, headerNames, headerOverwrite, headerRemove, headers, headersClear, httpVersion, httpVersion, initForm, isConnectionPersistent, isFormMultipart, mediaType, mediaType, populateHeaderAndBody, readBody, readHeaders, resolveFormEncoding, sendTo, toByteArray, tokenAuthentication, toString, toString, wrapFormValue
-
Field Details
-
protocol
-
host
-
port
protected int port -
method
-
fragment
-
path
-
query
-
queryEncoding
-
trustAllCertificates
private boolean trustAllCertificates -
verifyHttpsHost
private boolean verifyHttpsHost -
timeout
protected int timeout -
connectTimeout
protected int connectTimeout -
followRedirects
protected boolean followRedirects -
maxRedirects
protected int maxRedirects -
httpConnection
-
httpConnectionProvider
-
-
Constructor Details
-
HttpRequest
public HttpRequest()
-
-
Method Details
-
initRequest
protected void initRequest()Prepares request on creation. Does the following:- adds "Connection: Close" header.
- adds default headers
-
host
Returns request host name. -
host
Sets request host name. -
protocol
Returns used protocol. By default it's "http". -
protocol
Defines protocol. -
port
public int port()Returns request port number. When port is not explicitly defined, returns default port for current protocol. -
port
Sets request port number. -
fragment
Returns HTTP fragment, if specified; otherwise returnsnull. -
set
Sets the destination (method, host, port... ) at once. -
create
Generic request builder, usually used when method is a variable. Otherwise, use one of the other static request builder methods. -
connect
Builds a CONNECT request. -
get
Builds a GET request. -
post
Builds a POST request. -
put
Builds a PUT request. -
patch
Builds a PATCH request. -
delete
Builds a DELETE request. -
head
Builds a HEAD request. -
trace
Builds a TRACE request. -
options
Builds an OPTIONS request. -
method
Returns request method. -
method
Specifies request method. It will be converted into uppercase. Does not validate if method is one of the HTTP methods. -
method
-
path
Returns request path, without the query. -
path
Sets request path. Query string is allowed. Adds a slash if path doesn't start with one. Query will be stripped out from the path. Previous query is discarded.- See Also:
-
multipart
Forces multipart requests. When set tofalse, it will bedetectedif request should be multipart. By setting this totruewe are forcing usage of multipart request. -
cookies
Sets cookies to the request. -
query
Adds query parameter. -
query
Adds many query parameters at once. Although it accepts objects, each value will be converted to string. -
query
Adds all parameters from the provided map. -
query
Returns backend map of query parameters. -
clearQueries
Clears all query parameters. -
queryRemove
Removes query parameters for given name. -
queryString
- See Also:
-
queryString
Sets query from provided query string. Previous query values are discarded. -
queryString
Generates query string. All values are URL encoded. -
queryEncoding
Defines encoding for query parameters. -
url
-
hostUrl
Returns just host url, without path and query. -
basicAuthentication
Enables basic authentication by adding required header. -
tokenAuthentication
Enables token-based authentication. -
trustAllCerts
Trusts all certificates, use with caution. -
trustAllCertificates
public boolean trustAllCertificates()Returns a flag if to trusts all certificates. -
verifyHttpsHost
Verifies HTTPS hosts. -
verifyHttpsHost
public boolean verifyHttpsHost()Returns a flag if to verify https hosts. -
setHostHeader
Sets 'Host' header from current host and port. -
monitor
Registerslistenerthat will monitor upload progress. Be aware that the whole size of the request is being monitored, not only the files content. -
timeout
Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets). After establishing the connection, the client socket waits for response after sending the request. This is the elapsed time since the client has sent request to the server before server responds. Please note that this is not same as HTTP Error 408 which the server sends to the client. In other words its maximum period inactivity between two consecutive data packets arriving at client side after connection is established. A timeout value of zero is interpreted as an infinite timeout.- See Also:
-
timeout
public int timeout()Returns read timeout (SO_TIMEOUT) in milliseconds. Negative value means that default value is used.- See Also:
-
connectionTimeout
Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets). A timeout value of zero is interpreted as an infinite timeout. -
connectionTimeout
public int connectionTimeout()Returns socket connection timeout. Negative value means that default value is used.- See Also:
-
followRedirects
Defines if redirects responses should be followed. NOTE: when redirection is enabled, the original URL will NOT be preserved in the request! -
isFollowRedirects
public boolean isFollowRedirects()Returnstrueif redirects are followed. -
maxRedirects
Sets the max number of redirects, used whenfollowRedirectsis enabled. -
maxRedirects
public int maxRedirects()Returns max number of redirects, used whenfollowRedirectsis enabled. -
withConnectionProvider
Uses custom connection provider whenopeningthe connection. -
connectionProvider
Returns http connection provider that was used for creating current http connection. Ifnull, default connection provider will be used. -
connection
ReturnsHttpConnectionthat is going to be used for sending this request. Value is available ONLY after callingopen()and beforesend(). -
open
Opens a newconnectionusing either provided ordefaultconnection provider. -
open
Opens a newconnectionusing givenHttpConnectionProvider. -
open
Assignees providedHttpConnectionfor communication. It does not actually opens it until thesending. -
keepAlive
Continues using the same keep-alive connection. Don't use any variant ofopen()when continuing the communication! First it checks if "Connection" header exist in the response and if it is equal to "Keep-Alive" value. Then it checks the "Keep-Alive" headers "max" parameter. If its value is positive, then the existingHttpConnectionfrom the request will be reused. If max value is 1, connection will be sent with "Connection: Close" header, indicating its the last request. When new connection is created, the sameHttpConnectionProviderthat was used for creating initial connection is used for opening the new connection.- Parameters:
doContinue- set it tofalseto indicate the last connection
-
send
Opens connectionif not already open, sends request, reads response and closes the request. If keep-alive mode is enabled connection will not be closed. -
_reset
private void _reset()Resets the request by resetting all additional values added during the sending. -
_send
-
buffer
Prepares the request buffer.- Specified by:
bufferin classHttpBase<HttpRequest>
-
readFrom
Parses input stream and creates newHttpRequestobject. Assumes input stream is in ISO_8859_1 encoding. -
readFrom
-
contentTypeJson
Specifies JSON content type. -
acceptJson
Accepts JSON content type. -
sendAsync
Sends http request asynchronously using common fork-join pool. Note that this is not the right non-blocking call (not a NIO), it is just a regular call that is operated in a separate thread. -
sendAndReceive
Syntax sugar. -
sendAndReceive
Syntax sugar.
-