peroxide
peroxide

Reputation: 696

Getting status code of a website using socket connection

Hi
I'm connecting to a site using socket and i want to get the status code of the site.

Socket socket_1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result = socket_1.BeginConnect(iis1, 80, null, null);

How can i tell if the site is up or 404?
Thanks

Upvotes: 1

Views: 345

Answers (1)

driis
driis

Reputation: 164281

In order to do that using a socket connection, you will first need to form a valid http request and send it over the socket. Then read the servers response; parse the headers and look for the Status code. The spec for HTTP is here, and you will need to implement the parts of this you need, in order to do this with raw sockets.

That shouldn't be a problem, as it is fairly simple - But I wouldn't recommend doing this using sockets. There are much better options in the framework if you need to check site availability. The simplest being WebClient or HttpWebRequest.

If you just need to know about availability, you can send a HEAD request in order to only get the headers.

Upvotes: 6

Related Questions