1

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

Josh Harris
  • 177
  • 4
  • 15

3 Answers3

4

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.

thaBadDawg
  • 5,160
  • 6
  • 35
  • 44
0

See the answer on this question to do it using jquery. how-to-test-a-url-in-jquery

Community
  • 1
  • 1
Christoph
  • 4,251
  • 3
  • 24
  • 38
  • looks like the external link in the accepted answer changed, it's now http://binarykitten.me.uk/dev/jq-plugins/88-jquery-plugin-ajax-head-request.html – Christoph Mar 29 '10 at 16:00
  • Maybe I am just using it wrong but the plugin seems to be for local url checking. Meaning www.YourDomain.com/home.html, because if I try www.google.com. I get www.YourDomain.com/www.google.com. I am looking to check and see if a site like www.YourDomain.com is up from www.MyDomain.com. If I could do this on the client side that would be awesome because I believe I will have issues when the 3rd party sides are hosted in my customer's intranets. – Josh Harris Mar 29 '10 at 18:17
0

You are probably looking for the System.Net.HttpWebRequest class. You can use this class to make a request to the 3rd party system. If it is a ASP.Net web app, you are probably making this call from your code behind.

You will then want to check for a 200 response code. Things get a little trickier if you have to worry about security, or passing along the users cookies.

Trent
  • 560
  • 4
  • 11
  • Looks like theBadDawg beat me to it and included a code example as well. Nice job theBadDawg. – Trent Mar 29 '10 at 16:06