Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Link Validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Darren - 16 Oct 2007 14:41 GMT
Is there a way to determine a link to a file is valid (exists)?

I have found examples using httprequest, but I don't want to retrieve the
entire file, just verify it's existence.
These examples work very well when a link does not exist. But not so much on
valid ones. The files these links point to can be quite large in some cases
making such functions useless if the entire thing must be retrieved.

Thanks
Ujval Shah - 16 Oct 2007 15:37 GMT
Hi,

Try this

System.IO.FileInfo fileInfo = new System.IO.FileInfo("File Path");
if (fileInfo.Exists)
{
// Do this
}
else
{
  // Do this
}

> Is there a way to determine a link to a file is valid (exists)?
>
[quoted text clipped - 5 lines]
>
> Thanks
IfThenElse - 17 Oct 2007 00:17 GMT
He is talking about URLs

> Hi,
>
[quoted text clipped - 21 lines]
>>
>> Thanks
Patrice - 16 Oct 2007 17:53 GMT
Sending a HEAD request than a GET request should allow to get the header
rather than the whole content...

--
Patrice

> Is there a way to determine a link to a file is valid (exists)?
>
[quoted text clipped - 5 lines]
>
> Thanks
Darren - 16 Oct 2007 19:16 GMT
Works, thanks.

> Sending a HEAD request than a GET request should allow to get the header
> rather than the whole content...
[quoted text clipped - 12 lines]
>>
>> Thanks
IfThenElse - 17 Oct 2007 00:16 GMT
Darren,

How do you send a HEAD request than a GET
Can you Please share a small example.

Thank you,

> Works, thanks.
>
[quoted text clipped - 14 lines]
>>>
>>> Thanks
Rad [Visual C# MVP] - 18 Oct 2007 10:52 GMT
>Is there a way to determine a link to a file is valid (exists)?
>
[quoted text clipped - 5 lines]
>
>Thanks

Try setting the Method property to "HEAD" ... the web server will be
instructed not to send the entire body

--
http://bytes.thinkersroom.com
IfThenElse - 18 Oct 2007 17:11 GMT
How/Where do you set the Method property to "HEAD"
some details please, thank you,

>>Is there a way to determine a link to a file is valid (exists)?
>>
[quoted text clipped - 13 lines]
> --
> http://bytes.thinkersroom.com
Rad [Visual C# MVP] - 19 Oct 2007 11:11 GMT
>How/Where do you set the Method property to "HEAD"
>some details please, thank you,

No problem.

Here we go. I wrote a  small console application. You will need to add
references to System.Net and System.Web, as well as (System.IO) if you
want to retrieve the actual content

WebRequest r=null;
WebResponse res=null;
try{
    r = HttpWebRequest.Create("http://thesiteiwanttocheck.com");
    r.Method="HEAD";
    res = r.GetResponse();                 
}
catch(WebException ex){
    Console.WriteLine(ex.Status);
}
finally{
    if(res!=null)
        res.Close();
}

If the site cannot be accessed, a web exception could be thrown. You
can interrogate the status property to discover why. It could be a
problem from your end e.g. proxy authentication, etc or it could be
something wrong with the site. Check the status property before you
flag the site as unavailable!

Unless you say otherwise, the default method used by a HttpWebRequest
is GET. So before we get the response, we set the method as
appropriate.

To prove that the full body is not being retrieved, add the following
code just after GetResponse()

StreamReader sr = new StreamReader(res.GetResponseStream());
Console.Write(sr.ReadToEnd());

Then toggle the Method property between HEAD and GET and see what
happens.

Hope that helps

--
http://bytes.thinkersroom.com
IfThenElse - 19 Oct 2007 15:50 GMT
Rad,

Excellent and Thank YOU so much for the detailed code, Now I know what you
are saying.

It definitely helped me out.

>>How/Where do you set the Method property to "HEAD"
>>some details please, thank you,
[quoted text clipped - 43 lines]
> --
> http://bytes.thinkersroom.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.