TlsEcho

This sample connects to an HTTPS server, sends 'GET' request, receives a response and displays it.

Usage

The sample is a console utility that accepts an optional argument that specifies the target host and port.

> TlsEcho test.rebex.net:443

C#

// create an instance of TLS client socket
using (var socket = new TlsClientSocket())
{
    // log communication
    socket.LogWriter = new ConsoleLogWriter(LogLevel.Info);

    // connect to a server
    string serverName = "test.rebex.net";
    socket.Connect(serverName, 443);
    socket.Negotiate();

    // send a request
    string requestTemplate = "GET / HTTP/1.1\r\nHost: {0}\r\nConnection: close\r\n\r\n";
    byte[] request = Encoding.ASCII.GetBytes(string.Format(requestTemplate, serverName));
    socket.Send(request);

    // receive response and show it on console
    Console.WriteLine("---RESPONSE:---");
    byte[] response = new byte[4096];
    int count = socket.Receive(response);
    while (count > 0)
    {
        Console.Write(Encoding.UTF8.GetString(response, 0, count));
        count = socket.Receive(response);
    }
    Console.WriteLine();
}