Search code examples
c#ajaxweb-applicationscross-sitepinging

Can you cross-site ping another site using C# or JS/Ajax?


On our web application I am trying to ping a 3rd party site to see if it is up before redirecting our customers to it. So far I have not seen a way to do this other than from a desktop app or system console. Is this possible? I have heard that there was an image trick in original ASP.

Currently we are using .NET MVC with Javascript.

Thank you,

Josh


Solution

  • You can do a two stage process where you make an AJAX call and if it works then redirect to the site. For example, the AJAX call could do something like:

    public bool IsAddressResponsive(string Address)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Address);
        req.Method = "GET";
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        if (resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.Accepted)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    

    And if the response was true then redirect to the address.