简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM