-
Notifications
You must be signed in to change notification settings - Fork 50
/
SimpleBlockingClient.java
47 lines (37 loc) · 1.84 KB
/
SimpleBlockingClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package tlschannel.example;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import tlschannel.ClientTlsChannel;
import tlschannel.TlsChannel;
/** Client example. Connects to a public TLS reporting service. */
public class SimpleBlockingClient {
public static final String domain = "www.howsmyssl.com";
public static final String httpLine = "GET https://www.howsmyssl.com/a/check HTTP/1.0\nHost: www.howsmyssl.com\n\n";
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
// initialize the SSLContext, a configuration holder, reusable object
SSLContext sslContext = SSLContext.getDefault();
// connect raw socket channel normally
try (SocketChannel rawChannel = SocketChannel.open()) {
rawChannel.connect(new InetSocketAddress(domain, 443));
// create TlsChannel builder, combining the raw channel and the SSLEngine, using minimal options
ClientTlsChannel.Builder builder = ClientTlsChannel.newBuilder(rawChannel, sslContext);
// instantiate TlsChannel
try (TlsChannel tlsChannel = builder.build()) {
// do HTTP interaction and print result
tlsChannel.write(ByteBuffer.wrap(httpLine.getBytes(StandardCharsets.US_ASCII)));
ByteBuffer res = ByteBuffer.allocate(10000);
// being HTTP 1.0, the server will just close the connection at the end
while (tlsChannel.read(res) != -1) {
// empty
}
res.flip();
System.out.println(StandardCharsets.UTF_8.decode(res));
}
}
}
}