public class HttpClientConnection extends Object implements Closeable
| Constructor and Description |
|---|
HttpClientConnection(InetSocketAddress address)
Create an instance of HttpClientConnection.
|
HttpClientConnection(String host,
int port)
Create an instance of HttpClientConnection.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close the connection to the server
|
void |
connect()
Connects to the server.
|
String |
getNegotiatedCipher() |
TlsVersion |
getNegotiatedTlsProtocol() |
int |
getSocketSourcePort() |
X509Certificate[] |
getTlsCertificates() |
byte[] |
getTlsSessionId() |
void |
reset()
Reset the connection to the server
|
HttpResponse |
send(byte[] data)
This sends data to the server and waits for a response.
|
HttpResponse |
send(HttpRequestHeaders requestHeaders)
This sends a request without body and waits for a response.
|
HttpResponse |
send(HttpRequestHeaders requestHeaders,
byte[] body)
This sends a request with a body and waits for a response.
|
HttpResponse |
send(HttpRequestHeaders requestHeaders,
byte[] body,
BodyCompression compression)
This sends a request with a body and waits for a response.
|
HttpResponse |
send(HttpRequestHeaders requestHeaders,
InputStream body)
This sends a request with a body and waits for a response.
|
HttpResponse |
send(HttpRequestHeaders requestHeaders,
InputStream body,
BodyCompression compression)
This sends a request with a body and waits for a response.
|
HttpResponse |
send(HttpRequestHeaders requestHeaders,
String body)
This sends a request with a body and waits for a response.
|
HttpResponse |
send(HttpRequestHeaders requestHeaders,
String body,
BodyCompression compression)
This sends a request with a body and waits for a response.
|
void |
sendConnectRequest(String host)
This will send a CONNECT request and wait for a response, expecting to receive 200 OK.
|
HttpResponse |
sendRequest(String requestLine,
String... headers)
This sends a request without body and waits for response.
|
void |
setCipherSuites(String... enabledCipherSuites) |
void |
setTcpNoDelay(boolean tcpNoDelay)
Switch TCP_NODELAY
When it is TCP_NODELAY is on, Nagle's algorithm is switched off
|
void |
setTimeoutMillis(int timeout)
Sets timeout for IO operations on socket
|
void |
setTlsProtocol(TlsVersion... enabledTlsProtocols) |
void |
startHandshake()
Start TLS connection without validating certificates received from the server.
|
void |
startHandshake(String hostName)
Start TLS connection without validating certificates received from the server sending specified host name as part
of ClientHello.
|
void |
startHandshakeAndValidate()
Start TLS connection validating certificates received from the server using default trust manager.
|
void |
startHandshakeAndValidate(String hostName)
Start TLS connection validating certificates received from the server using default trust manager.
|
void |
startHandshakeWithClientAuth(String hostName,
String keyStorePath,
String keyStorePassword,
String certificatePassword)
Start TLS connection without validating certificates received from the server sending specified host name as part
of ClientHello and a certificate for client validation.
|
void |
startHandshakeWithClientAuthAndValidate(String hostName,
String keyStorePath,
String keyStorePassword,
String certificatePassword)
Start TLS connection validating certificates received from the server sending specified host name as part of
ClientHello and a certificate for client validation.
|
void |
useJdkDefaultCipherSuites() |
void |
useJdkDefaultTlsProtocols() |
public HttpClientConnection(String host, int port)
host - - host name (could be a string contains an IP address), if we are connecting via a proxy, this should
be proxy hostname/IPport - - port (e.g. 80 for HTTP or 443 for HTTPS)public HttpClientConnection(InetSocketAddress address)
address - - IP socket address to the server we will be connecting. If we connecting via a proxy, this should be
an address of the proxy.public HttpResponse sendRequest(String requestLine, String... headers) throws IOException
requestLine - - request line of the request (e.g. "GET /path HTTP/1.1")headers - - headers (e.g. "Accept-Encoding: gzip")IOExceptionpublic HttpResponse send(HttpRequestHeaders requestHeaders) throws IOException
requestHeaders - - request headers that will be sent to the serverIOExceptionpublic HttpResponse send(HttpRequestHeaders requestHeaders, String body) throws IOException
requestHeaders - - request headers that will be sent to the serverbody - - a body of the requestIOExceptionpublic HttpResponse send(HttpRequestHeaders requestHeaders, String body, BodyCompression compression) throws IOException
requestHeaders - - request headers that will be sent to the serverbody - - a body of the requestcompression - - specifies if body needs to be compressedIOExceptionpublic HttpResponse send(HttpRequestHeaders requestHeaders, byte[] body) throws IOException
requestHeaders - - request headers that will be sent to the serverbody - - a body of the requestIOExceptionpublic HttpResponse send(HttpRequestHeaders requestHeaders, byte[] body, BodyCompression compression) throws IOException
requestHeaders - - request headers that will be sent to the serverbody - - a body of the requestcompression - - specifies if body needs to be compressedIOExceptionpublic HttpResponse send(HttpRequestHeaders requestHeaders, InputStream body) throws IOException
requestHeaders - - request headers that will be sent to the serverbody - - an input stream from where to read data for the request body (the data is read until end of stream
encountered)IOExceptionpublic HttpResponse send(HttpRequestHeaders requestHeaders, InputStream body, BodyCompression compression) throws IOException
requestHeaders - - request headers that will be sent to the serverbody - - an input stream from where to read data for the request body (the data is read until end of stream
encountered)compression - - specifies if body needs to be compressedIOExceptionpublic HttpResponse send(byte[] data) throws IOException
data - - data to be sent to the server as-isIOExceptionpublic void sendConnectRequest(String host) throws IOException
host - - host name to be used in the requestIOException - - when IO failed or the proxy returned a result different from 200.public void startHandshake()
throws IOException
IOExceptionpublic void startHandshake(String hostName) throws IOException
hostName - - host name to be sent in SNI in ClientHello. This is needed for the server to choose a correct
certificate, if null - do not set SNIIOExceptionpublic void startHandshakeWithClientAuth(String hostName, String keyStorePath, String keyStorePassword, String certificatePassword) throws IOException
hostName - - host name to be sent in SNI in ClientHello. This is needed for the server to choose a correct
certificate, if null - do not set SNIkeyStorePath - - path to Java keystore file (JKS file)keyStorePassword - - password for Java keystore filecertificatePassword - - password for certificates in Java keystoreIOExceptionpublic void startHandshakeAndValidate()
throws IOException
IOExceptionpublic void startHandshakeAndValidate(String hostName) throws IOException
hostName - - host name to be sent in SNI in ClientHello. This is needed for the server to choose a correct
certificate.IOExceptionpublic void startHandshakeWithClientAuthAndValidate(String hostName, String keyStorePath, String keyStorePassword, String certificatePassword) throws IOException
hostName - - host name to be sent in SNI in ClientHello. This is needed for the server to choose a correct
certificate, if null - do not set SNIkeyStorePath - - path to Java keystore file (JKS file)keyStorePassword - - password for Java keystore filecertificatePassword - - password for certificates in Java keystoreIOExceptionpublic void close()
close in interface Closeableclose in interface AutoCloseablepublic void reset()
public X509Certificate[] getTlsCertificates()
public TlsVersion getNegotiatedTlsProtocol()
public String getNegotiatedCipher()
public byte[] getTlsSessionId()
public void setTlsProtocol(TlsVersion... enabledTlsProtocols)
enabledTlsProtocols - - list of allowed TLS protocolspublic void useJdkDefaultTlsProtocols()
public void setCipherSuites(String... enabledCipherSuites)
enabledCipherSuites - - list of allowed Cipher Suitespublic void useJdkDefaultCipherSuites()
public int getSocketSourcePort()
public void connect()
throws IOException
IOExceptionpublic void setTimeoutMillis(int timeout)
throws SocketException
timeout - - timeout in millisecondsSocketException - - when there is an error in TCP protocolpublic void setTcpNoDelay(boolean tcpNoDelay)
throws SocketException
tcpNoDelay - - when true then Nagle's algorithm is off (default)SocketExceptionCopyright © 2019. All rights reserved.