Joe Fred
Joe Fred

Reputation: 33

Get only http status code with sockets

I have to check the http status codes of some domains, with the smallest amount of traffic is possible.

I decided to use sockets. Problem is that the system is receiving allways the full header and not only 20 byte.

How can i reduce the response more?

Here is the code...works fine

    string uri = "www.stackoverflow.com";
    var addresses = System.Net.Dns.GetHostAddresses(uri);
    IPEndPoint hostep = new IPEndPoint(addresses[0], 80);
    Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    sock.Connect(hostep);
    if (sock.Connected)
    {
        byte[] msg = Encoding.UTF8.GetBytes("HEAD / HTTP/1.1\r\nHost: "+uri+"\r\nConnection: Close\r\n\r\n");
        int i = sock.Send(msg,0,msg.Length);
        byte[] bytes = new byte[20];
        i = sock.Receive(bytes);
        string header = Encoding.ASCII.GetString(bytes);
        Console.WriteLine(header);
    }

Thanks for each small help!

Upvotes: 0

Views: 1574

Answers (1)

Joker_vD
Joker_vD

Reputation: 3765

i = sock.Receive(bytes, 20);

However, that's pointless: the computer most likely has already received the TCP-packet with the full response, and all you do is just read only a part of the sytem buffer.

Upvotes: 1

Related Questions