Good morning, does anyone know how to detect whether a url link is valid or not. I was ask to create a function that detects whether a url link is valid or not, and if it is not valid then certain actions take place, but for now i am not sure how to detect the url, i have tried many coding but it seems to not work at all. please help me!!
Trying to detect whether an url link is valid or not
Collapse
X
-
By 'valid' do you mean it's formatted correctly it a URL that actually exists? You say you've tried some coding but cannot get it to work, show us what you've tried and it'll be easier for us to help. -
This is the coding i did, but whether it's only http or with www, it only displays Invalid URL. eg. https://www.google.com.my/ & http://forums.asp.net/
Code:protected void Page_Load(object sender, EventArgs e) { Uri url = new Uri("http//..."); WebRequest request = WebRequest.Create(url); request.Method = "HEAD"; WebResponse r; try { r = request.GetResponse(); r.Close(); Response.Write("Valid URL"); } catch (Exception) { Response.Write("Invalid URL"); } }Comment
-
Try changing your code to this (it will then show you what's going wrong:
Code:protected void Page_Load(object sender, EventArgs e) { Uri url = new Uri("http//..."); WebRequest request = WebRequest.Create(url); request.Method = "HEAD"; WebResponse r; try { r = request.GetResponse(); r.Close(); Response.Write("Valid URL"); } catch (Exception ex) { //Show the exact error Response.Write(ex.ToString()); } }Comment
-
another question i have is, is it possible to add gridview inside the try-catch statement, like this:
Code:try { r = request.GetResponse(); r.Close(); GridView1.DataSource = RSS(url); GridView1.DataBind(); } catch (Exception) { Response.Write("Invalid URL"); }Comment
Comment