1

I have created a method in windows services in asp.net application.

       public static bool Test(string url)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "HEAD";
        try
        {
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            resp.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

        JurisdictionBL jbl = new JurisdictionBL(0);
        DataSet jds = new DataSet();
        jbl.FetchAll(jds);


        foreach (DataTable table in jds.Tables)
        {
            foreach (DataRow dr in table.Rows)
            {
                var jId = dr["Jurisdiction"].ToString();
                var jurl = dr["URL"].ToString();
                if (Test(jurl))
                {
                    jbl.IsValidUrlTrue(Convert.ToInt32(jId));
                }
                else
                {
                    jbl.IsValidUrlFalse(Convert.ToInt32(jId));
                }
            }
        }

The whole application does not work. It gets stuck. Basically I have a field "IsValidURL" in table tbJurisdiction and a field URL which needs to be constantly checked whether they are working or not. If they are not working then IsValidURL should become false. Also It is not checking any url properly. If any url is not valid it still display isvalidURL Feild true

Please Help me !!!

  • You just want to valiate URL format or want to check if the url is alive? – Vijay Kumbhoje Nov 27 '15 at 05:19
  • I want to check if the url is alive? –  Nov 27 '15 at 05:23
  • It gets stuck, where? Probably at req.GetResponse() ? at jbl.FetchAll() ? – Oguz Ozgul Nov 27 '15 at 05:25
  • Yes at req.GetResponse(). It gets stuck in Test method()... –  Nov 27 '15 at 05:28
  • How many Id s you have in the list of jurisdictions you have there? You are making blocking calls to a web server for the whole list synchronously, it is obvious that if you have many, your application will wait for a significant while without any UI update. Try using asynchronous methods provided by the httpwebrequest class. You should definitely change your app to check for the availability of these Url s a asynchronously – Oguz Ozgul Nov 27 '15 at 05:29
  • If you are stuck getting response for a specific Id then it is a different thing. Are you getting any response for your HEAD requests at all? Not all servers may respond to that I think. – Oguz Ozgul Nov 27 '15 at 05:31
  • There are more than 30 Jurisdictions.... Can you please help me in how to create asynchronos method... –  Nov 27 '15 at 05:33
  • Try this one http://stackoverflow.com/a/3808841/3583859 – Vijay Kumbhoje Nov 27 '15 at 05:33
  • Possible duplicate of [C# How can I check if a URL exists/is valid?](http://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid) – Vijay Kumbhoje Nov 27 '15 at 05:35
  • Now the application does not stuck... It still displays true to inactive url Please help ... –  Nov 27 '15 at 05:49

1 Answers1

0

You will need to check the status code of the response to validate if the URL is providing your requested resources.

public static bool Test(string url)
{

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "HEAD";
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    Boolean isHttpOk = resp.StatusCode == HttpStatusCode.OK;
    resp.Close();

    return isHttpOk;

}
Yanana
  • 7
  • 5
PythaLye
  • 314
  • 1
  • 8