send(), Jodd HTTP will
open()connection if not already opened. HTTP connections are created by the connection provider instance:
HttpConnectionProvider. The default connection provider is socket-based and it always returns a new
SocketHttpConnectioninstance - that simply wraps a
Socketand opens it.
HttpConnectionProvider, based on default implementation. For example, you may extend the
createSocket()method to return sockets from some pool, or sockets with a different timeout.
HttpConnectiondirectly, without any provider.
Socket. Since it is a common need to tweak socket behavior before sending data, here are two ways of how you can do it with Jodd HTTP.
HttpConnection, we can simply get the instance after explicitly calling the
open()and cast it:
SocketHttpConnectionProvider. So you may create your own provider like this:
HttpConnectionis opened on the first request and then re-used in communication session; the socked is not opened again if not needed and therefore it is reused for several requests.
HttpConnection(i.e. the same socket). When in 'keep-alive' mode, HTTP continues using the existing connection, while paying attention to server responses. If the server explicitly requires a connection to be closed, HTTP will close it and then it will open a new connection to continue your session. You don't have to worry about this, just keep calling
keepAlive()and it will magically do everything for you in the background. Just don't forget to pass
falseargument to the last call to indicate the server that is the last connection and that we want to close after receiving the last response. (if for some reasons the server does not respond correctly, you may close communication on the client-side with an explicit call to
response.close()). One more thing - if a new connection has to be opened during this persistent session (when e.g. keep-alive max counter is finished or timeout expired) the same connection provider will be used as for the initial, first connection.
HttpConnectionProvideralso allows you to specify the proxy. Just provide the
ProxyInfoinstance with the information about the used proxy (type, address, port, username, password):
HttpResponsehave a method
readFrom(InputStream). Basically, you can parse the input stream with these methods. This is, for example, how you can read the request on server-side.