SSL Socket Request C#

Hi,

I have issue communicating with SSL sites. I would like to receive data from a SSL (HTTPS) site. I am using Socket class in C# my code is similar to the code below. As you seen there are no indication of how to behave with a SSL request.

class MyClient

{

public static void MyGet(string sHost, int iPort)

{

IPHostEntry IPHost = Dns.Resolve(sHost);

Console.WriteLine(IPHost.HostName);

IPAddress[] addr = IPHost.AddressList;

Console.WriteLine(addr[0]);

EndPoint ep = new IPEndPoint(addr[0], iPort);

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

sock.Connect(ep);

if (sock.Connected)

Console.WriteLine("OK");

Encoding ASCII = Encoding.ASCII;

string Get = "GET / HTTP/1.1\r\nHost: " + sHost + ":" + iPort + "\r\nConnection: Close\r\n\r\n";

Byte[] ByteGet = ASCII.GetBytes(Get);

Byte[] RecvBytes = new Byte[256];

sock.Send(ByteGet, ByteGet.Length, 0);

Int32 bytes = sock.Receive(RecvBytes, RecvBytes.Length, 0);

Console.WriteLine(bytes);

String strRetPage = null;

strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

while (bytes > 0)

{

bytes = sock.Receive(RecvBytes, RecvBytes.Length, 0);

strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);

Console.WriteLine(strRetPage);

}

sock.ShutDown(SocketShutdown.Both);

sock.Close();

}

}

Thanks


Answer this question

SSL Socket Request C#

  • MARCEL MEDINA

    I also recommend you use HttpWebRequest for Https requests as implemeting a HTTP stack using SslStream is not trivial. This will save you many months of work.

  • StevePO

    You can't use just sockets to get to SSL sites.
    Sockets is just a connection and data send receive
    SSL is on top of sockets.
    HTTP is on top of SSL.

    As such,

    1) You should HTTPWebRequest.
    2) You can use SSLStream class that works on top of sockets but then
    You need to implement http yourself which I don't recommend



  • John_NSI

    Oh ok!

    1. It looks like SSLStream is a 3rd party object, it is not build in .NET

    2. Why should I not implement http

    3. Are you recomending HTTPWebRequest becuase it is hiding these from us What is the down side of the HTTPWebRequest

    Thanks

    Anoush


  • MikD454

    SSL Stream is part of the .NEt 2.0

    Check it out. It can save you months of work. No kidding.

    TcpClient will not help



  • Carlos Marín

    Thank you for your suggestions, but what is the downside of the HttpWebRequest It looks like there is a performance issue, when I compare it with Socket. We don’t have few months.

    Anoush


  • AntoineF

    How about the TCPCleint Class can it help me with this task

    Anoush


  • SSL Socket Request C#