473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trying to detect whether an url link is valid or not

8
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!!
Sep 3 '12 #1
4 2022
PsychoCoder
465 Expert Mod 256MB
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.
Sep 3 '12 #2
colol
8
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/

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.  
  4.         Uri url = new Uri("http//...");
  5.         WebRequest request = WebRequest.Create(url);
  6.         request.Method = "HEAD";
  7.         WebResponse r;
  8.  
  9.         try
  10.         {
  11.             r = request.GetResponse();
  12.             r.Close();
  13.             Response.Write("Valid URL");
  14.         }
  15.         catch (Exception)
  16.         {
  17.             Response.Write("Invalid URL");
  18.         }
  19.  
  20.     }
Sep 3 '12 #3
PsychoCoder
465 Expert Mod 256MB
Try changing your code to this (it will then show you what's going wrong:

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.  
  4.         Uri url = new Uri("http//...");
  5.         WebRequest request = WebRequest.Create(url);
  6.         request.Method = "HEAD";
  7.         WebResponse r;
  8.  
  9.         try
  10.         {
  11.             r = request.GetResponse();
  12.             r.Close();
  13.             Response.Write("Valid URL");
  14.         }
  15.         catch (Exception ex)
  16.         {
  17.             //Show the exact error
  18.             Response.Write(ex.ToString());
  19.         }
  20.  
  21.     }
Sep 3 '12 #4
colol
8
another question i have is, is it possible to add gridview inside the try-catch statement, like this:

Expand|Select|Wrap|Line Numbers
  1. try 
  2.         { 
  3.             r = request.GetResponse(); 
  4.             r.Close(); 
  5.             GridView1.DataSource = RSS(url);
  6.             GridView1.DataBind();
  7. }
  8. catch (Exception) 
  9.         { 
  10.             Response.Write("Invalid URL"); 
  11.         }
Sep 3 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: selowan | last post by:
Hi, In VB6 Pro SP5, I have a form that contains an MSFlexGrid and a few other textboxes and comboboxes. I am using the technique from MSDN article Q241355, which describes how to tab and edit in...
9
by: florent | last post by:
I'm trying to parse html documents from the web, using the HTMLParser class of the HTMLParser module (python 2.3), but some web documents are not fully valids. When the parser finds an invalid tag,...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
3
by: zZ | last post by:
Hi All, I need to detect the framework installed from both VB.Net and VB6. Can someone give me an hint? Thanks for any tip. Kind regards, Zen
1
by: yxq | last post by:
Hello, I have a main form, there other some controls(i.e. textbox) in the form. I want to detect whether Caps Lock key is pressed in form-key event. Private Sub Form1_KeyUp(ByVal sender As...
3
by: umdsasha | last post by:
So, basically, I need to detect whether an alert window was thrown. I can't find where it's thrown from but I need to disable a button only if there were no alert windows thrown. Any ideas? ...
1
by: pemigh | last post by:
I'm almost done with an application, and trying to lock it down tightly. But I still want users to be able to point to a new location for the data file. The code below fires off to detect and...
1
by: poornimaniranjan | last post by:
Hello... I am new to VC++ environment..I am able to detect the CD-ROM drive using GetDriveType API . but this information is not enough for me. I need to know whether the drive is writer or...
7
by: Baron Samedi | last post by:
I coded up a hit counter, then extended it to see who was reading my blog, by matching IP. The problem is that I am swamped by crawlers. How can I detect a human, or a crawler? If I can handle...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.