3

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

hitarth
  • 317
  • 4
  • 5
  • 19
  • There is much more stuff to validate, have a look at this [Python question](http://stackoverflow.com/questions/827557/how-do-you-validate-a-url-with-a-regular-expression-in-python) – oleksii Apr 18 '13 at 09:52
  • @oleksii i have put that regular expressio but doesn't check that is valid or not or it exist or not – hitarth Apr 18 '13 at 09:54
  • If you need to know whether it is active/exists, you should access it. That's the only way to tell, and even that is not guaranteed (you may be faced with a paywall for wifi access, for example, or a proxy server's 404 page - or there may be network issues which prevent you from reaching the URL you are attempting to visit) – penguat Apr 18 '13 at 10:33

2 Answers2

3

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;
MSTdev
  • 4,507
  • 2
  • 23
  • 40
  • can you please edit your code according to my code...because uri name doesnt exist – hitarth Apr 18 '13 at 10:00
  • ok sure `string uriName = urltxt.Text; bool Uriresult = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult) && uriName.Scheme == Uri.UriSchemeHttp;` – MSTdev Apr 18 '13 at 10:18
  • can you please tell me what is uriResult in out uriResult – Sivajith Jan 09 '15 at 06:56
3

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");
            }
        }
hitarth
  • 317
  • 4
  • 5
  • 19
Pandian
  • 8,848
  • 2
  • 23
  • 33
  • thanks alot bro....its working ........i got problem just see edit – hitarth Apr 18 '13 at 10:33
  • hey @pandian if want to also check the mail and https whether they are valid or not..how can i do in this code so i can check whether it is working or not 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; } } – hitarth Apr 23 '13 at 06:30
  • Pandian how to do that...thanks in advance in bro – hitarth Apr 23 '13 at 06:47