1

Need to implement SSL with Socket connection class in c# to get the web page. I searched on google but not found any proper code snippets I am able to get data without ssl

        string request = "GET /search?q=devendra&gws_rd=cr&ei=sBkRV4JjxfS6BKP9ieAN HTTP/1.1\r\nHost: " + server +
            "\r\nConnection: Close\r\n\r\n";
        Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
        Byte[] bytesReceived = new Byte[256];

        // Create a socket connection with the specified server and port.
        Socket s = ConnectSocket(server, port);

        if (s == null)
            return ("Connection failed");


        NetworkStream nStream = new NetworkStream(s, true);

        // Wrap in SSL stream

        SslStream ssStream = new SslStream(nStream);
        ssStream.AuthenticateAsClient(server);
        // Send request to the server.
        s.Send(bytesSent, bytesSent.Length, 0);

        // Receive the server home page content.
        int bytes = 0;
        string page = "Default HTML page on " + server + ":\r\n";

        // The following will block until te page is transmitted.
        do
        {
            bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
            page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
        }
        while (bytes > 0);

        return page;

Getting error on AuthenticateClient method of SSLStream :

The handshake failed due to an unexpected packet format.

mpromonet
  • 11,326
  • 43
  • 62
  • 91

0 Answers0