简体   繁体   中英

Check URL is working or not using c#.net

while i am check URL using Web request and web Response method.

bool result = false;
        try
        {
            if (!url.Contains("http://")) url = "http://" + url;
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "HEAD";
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            { if (response.StatusCode == HttpStatusCode.OK) result = true; }
        }
        catch
        {
            return result;
        }
        return result;

But while i am check into this URL " http://payments.rctrustee.org/ " am getting error. so i get the result is false. But i check into browser this URL is working.

i need this result true; how can i change my code.

Check with the following code:

try
            {
               if (!url.Contains("http://")) url = "http://" + url;
                WebRequest req = WebRequest.Create(url);

                WebResponse res = req.GetResponse();

                Console.WriteLine("Url Exists");
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
                if (ex.Message.Contains("remote name could not be resolved"))
                {
                    Console.WriteLine("Url is Invalid");
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM