tags:

views:

50

answers:

1

namespace WebApplication4 { public partial class _Default : System.Web.UI.Page {

    public static bool UrlIsValid(string url)
    {

        bool br = false;
        try
        {
            IPHostEntry ipHost = Dns.Resolve(url);
            br = true;
        }
        catch (SocketException)
        {
            br = false;
        }
        return br;
    }


    private void Page_Load(object sender, EventArgs e)
    {

        string url = "http://www.srv123.com";

       WebRequest wr = WebRequest.Create(url); 

using (HttpWebResponse response = (HttpWebResponse)wr.GetResponse ()) { if (response.StatusCode == HttpStatusCode.OK) { response.Close();

    Response.Redirect(url);
}
else
{
    Response.Redirect("http://www.yahoo.com");
}

when i give google it redirects to google but when i giv an invalid url it doesn redirect to yahoo lik hw i hav given. i gave the response before redirect

+1  A: 

Your UrlIsValid takes a parameter called smtpHost, suggesting it works with SMTP mail servers. Then within it, you attempt to resolve the entire URL as a hostname. It simply won't work at all.

You could create a new Uri object with the URL in, then create a WebRequest object, see the example here:

http://msdn.microsoft.com/en-us/library/system.uri.aspx

Uri siteUri = new Uri("http://www.contoso.com/");
WebRequest wr = WebRequest.Create(siteUri);

// now, request the URL from the server, to check it is valid and works
using (HttpWebResponse response = (HttpWebResponse)wr.GetResponse ())
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        // if the code execution gets here, the URL is valid and is up/works
    }
    response.Close();
}

Hope that helps!

Kieren Johnstone
HttpWebResponse implements IDisposable so you could wrap that up in a using block.
fletcher
@fletcher Thanks
Kieren Johnstone
thanx for your reply.. but i want it to redirect. and more over this is not working .. pleas suggest how to redirect
Shweta
Then add a Response.Redirect, that will redirect to the URL of your choosing; your question shows how to use it. Could you try the code again and give more information as to the problem you are having? It demonstrates the concept very well, I think
Kieren Johnstone
I used the code ut i don know wher to use response.redirect. and i used response.redirect(Uri)it doesn seem to work then
Shweta
could you please add up the response code for me
Shweta
i added the code but it redirects to google but if i give a wrong url it doesn redirect to the second website provided
Shweta
The code I have there will determine whether a URL is accessible or not; if you are having a problem integrating it could you update your question with the code integrated into your project (to show how you have integrated it) please?
Kieren Johnstone
i hav updated it .
Shweta
Your code is invalid and will not compile - you have two Page_Load method signatures, for example (remove the first Page_Load and subsequent { line). The 'response.Close()' line sohuld go before the 'Response.Redirect' line. That code should work just fine; if you set a breakpoint with F9 on the WebRequest line and press F10 to see where the execution path leads, you should be able to see exactly what code is executing: if you still have a problem please post what you found here
Kieren Johnstone
i actually dont hav two page load, when i copy pasted it jus came by mistake. i ll check with that response thing
Shweta
i did it but the same issue as i said earlier
Shweta
Check the Response property of the exception to determine why the request failed. this is the error i am gettin when i am puttin a break . i checked that httpresponse area. and the error s over dere
Shweta