I am trying to verify if a website is up or not. i am using TcpClient to make a call to website and it working fine but when i use webrequest for same site it throws a 404 error.
what is the difference in the functionality of TcpClient and webRequest??
var client = new TcpClient();
client.Connect("android.clients.google.com", 80);
var request = WebRequest.Create("http://android.clients.google.com");
var resp = request.GetResponse();
what is the difference in the functionality of TcpClient and webRequest??
A TcpClient
is a wrapper around a TCP client socket, letting you communicate with any TCP server. The application protocol used to communicate with that server is to be implemented by your code. Your code merely connects to the HTTP server, but doesn't communicate with it.
An HttpWebRequest
handles not only the TCP layer, but also the HTTP layer. This lets you execute web requests to any HTTP server. Your code executes a GET
request to http://android.clients.google.com
, which doesn't exist, hence the 404
you receive.
To address your original issue: the fact that a web server responds to socket connections on a given port or even returns an HTTP response, may or may not mean the site is considered "up", and so does getting a 200 response. Does receiving Twitter's "failwhale" mean the site is "up"? Depending on what you're actually trying to do, different approaches may be required. Sometimes it's just best to fire the request you want to, instead of "pinging" the site on beforehand.