Nick Fulton
Nick Fulton

Reputation: 333

How would I check to see if website a is online

So, I know it's possible to ping a server, with Ping.Send(string) in c#, but would it be possible to ping a specific web application, instead of the whole server. For example, there is a server with three websites (a, b, and c) hosted on it. The server IP is 1.1.1.1. Each of the websites has a different port. How would I check to see if website a is currently being hosted?

Upvotes: 2

Views: 679

Answers (2)

loneshark99
loneshark99

Reputation: 714

I wouldnt do the GetResponse() because, what if that particular Url is returning you a 1+ GB of file, this will block your application. Just making the head request should be sufficient or using TcpClient.

async Task<Boolean> IsAvailable()
{
    string url = "http://www.google.com";
    try
    {
        using (var client = new HttpClient())
        {
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, url);
            var response = await client.SendAsync(request);
            if (response.IsSuccessStatusCode)
            {
                response.Dump();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    catch (Exception ex)
    {
      return false;
    }
}

Upvotes: 2

Alberto Monteiro
Alberto Monteiro

Reputation: 6219

Just make a request to WebSite

private bool PingWebSite(string url)
{
    try
    {
        WebRequest.Create(url).GetResponse();
        return true;
    }
    catch
    {
        return false;
    }
}

And then use it

var isWebSiteWorking = PingWebSite("http://stackoverflow.com");

Upvotes: 5

Related Questions