How to check whether the URL is valid or not

I have one textbox in that user enter the URL, but if want to check that URL while page rendering then what to do?

Here is my code:

protected void btnRender_Click(object sender, EventArgs e)
{
    string strResult = string.Empty;
    WebResponse objResponse;
    WebRequest objRequest = System.Net.HttpWebRequest.Create(urltxt.Text);
    objResponse = objRequest.GetResponse();
    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
    {
        strResult = sr.ReadToEnd();
        sr.Close();
    }
    strResult = strResult.Replace("<form id='form1' method='post' action=''>", "");
    strResult = strResult.Replace("</form>", "");          
    TextBox1.Text = strResult.Trim();
    div.InnerHtml = strResult.Trim();
}

I have this code to check whether URL is valid or not, so can you please tell me where to call this?
{if i want to also check https also then how can i do in this code}

 protected bool CheckUrlExists(string url)
    {
        // If the url does not contain Http. Add it.
      // if i want to also check for https how can i do.this code is only for http not https
        if (!url.Contains("http://"))
        {
            url = "http://" + url;
        }
        try
        {
            var request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "HEAD";
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                return response.StatusCode == HttpStatusCode.OK;
            }
         }
         catch
         {
             return false;
         }
     }

The TextBox name is urltxt

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try like below, It will help you….

     protected void btnRender_Click(object sender, EventArgs e)
        {
            if(CheckUrlExists(urltxt.Text))
            {
                string strResult = string.Empty;
                WebResponse objResponse;
                WebRequest objRequest = System.Net.HttpWebRequest.Create(urltxt.Text);
                objResponse = objRequest.GetResponse();
                using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                {
                    strResult = sr.ReadToEnd();
                    sr.Close();
                }
                strResult = strResult.Replace("<form id='form1' method='post' action=''>", "");
                strResult = strResult.Replace("</form>", "");
                TextBox1.Text = strResult.Trim();
                div.InnerHtml = strResult.Trim();
            }
            else
            {
                MessageBox.Show("Not a Valid URL");
            }
        }

Method 2

Try this uriName(your desired URI)

  bool Uriresult = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriName.Scheme == Uri.UriSchemeHttp;

as per your code

string uriName = urltxt.Text; bool Uriresult = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriName.Scheme == Uri.UriSchemeHttp;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x