469,613 Members | 1,827 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

Checking URL for file in VB.net how can it be done?

Ok. Maybe I shouldnt post such basic questions here in such an advanced
group but my high school programming teacher wont answer any questions
outside of his curriculum :(

My goal is create a program that will get feeded a url that links direcly
to a file [ex. http://www.example.com/video.avi] the program will then
determine if the file is there or not.

There are probably more efficient ways of doing what I want to do but this
is the way I have in mind:

1. dump the url's text into VB, in other words: what is actually in the
webpage. (Most likely since the file will not be there the string "File
could not be found" will be given to vb)

2. perform an [indexOf("File could not be found")] on the dumped text to
see if there if the file is acutually there.

3. if the indexOf gives a -1 that means that there was never a "file not
here" on the webpage therefore the file IS there!

Wow that was hard to explain. Please keep in mind that i'm in my first year
of programming in high school so please if you can explain in easier terms
is will be better for me to understand.

as a less important side question:
can vb DO things with websites? for example: can i write a program that
will actually fill out forms with correct data and submit them? what I have
in mind is writing programs that will send me an SMS when it is done doing
a task (using google send to phone:
http://toolbar.google.com/send/sms/index.php)
Feb 1 '07 #1
6 2417
Daniel Padron wrote:
Ok. Maybe I shouldnt post such basic questions here in such an advanced
group but my high school programming teacher wont answer any questions
outside of his curriculum :(
the reason you should not post ""here"" is not that at all.

It is because of the two groups you have chosen, one of which does NOT cover dotnet.

Followups set to the dotnet group ONLY.

Bob
--
Feb 1 '07 #2
Daniel Padron <ng*********@spamgourmet.comwrote:
Ok. Maybe I shouldnt post such basic questions here
Well, please note that VB.net and VB are *entirely* different languages, and each
have their own set of dedicated groups on this server. For the product you're
using, you'll find groups that contain ".dotnet." in their names to be most useful.

Good luck... Karl
--
..NET: It's About Trust!
http://vfred.mvps.org
Feb 1 '07 #3
It is because of the two groups you have chosen, one of which does NOT
cover dotnet.

Followups set to the dotnet group ONLY.
fair enough. sorry.
Feb 1 '07 #4
Daniel,

Maybe will this help you, it is not an answer on your question, but the
parts are in it that you probably need.
http://www.vb-tips.com/dbpages.aspx?...f-56dbb63fdf1c

Cor

"Daniel Padron" <ng*********@spamgourmet.comschreef in bericht
news:Xn********************@216.77.188.18...
Ok. Maybe I shouldnt post such basic questions here in such an advanced
group but my high school programming teacher wont answer any questions
outside of his curriculum :(

My goal is create a program that will get feeded a url that links direcly
to a file [ex. http://www.example.com/video.avi] the program will then
determine if the file is there or not.

There are probably more efficient ways of doing what I want to do but this
is the way I have in mind:

1. dump the url's text into VB, in other words: what is actually in the
webpage. (Most likely since the file will not be there the string "File
could not be found" will be given to vb)

2. perform an [indexOf("File could not be found")] on the dumped text to
see if there if the file is acutually there.

3. if the indexOf gives a -1 that means that there was never a "file not
here" on the webpage therefore the file IS there!

Wow that was hard to explain. Please keep in mind that i'm in my first
year
of programming in high school so please if you can explain in easier terms
is will be better for me to understand.

as a less important side question:
can vb DO things with websites? for example: can i write a program that
will actually fill out forms with correct data and submit them? what I
have
in mind is writing programs that will send me an SMS when it is done doing
a task (using google send to phone:
http://toolbar.google.com/send/sms/index.php)

Feb 1 '07 #5
Daniel Padron wrote:
<snip>
My goal is create a program that will get feeded a url that links direcly
to a file [ex.http://www.example.com/video.avi] the program will then
determine if the file is there or not.
<snip>

It depends on what you really want with the "file" (not always the
link points really to the file. Sometimes the server performs some
processing before actually retrieving the "file", and may actually
return something completely different).

If you just want to check that the link is valid, then you may
consider using the System.Net.HttpRequest class:

<code>
'This must be at the file level:
Imports Net = System.Net
'...
'...
'...
'This would be in a method:
' Initializes the request
Dim Link As String = "http://www.example.com/video.avi"
Dim Req As Net.HttpWebRequest = _
CType(Net.WebRequest.Create(Link), Net.HttpWebRequest)
Req.Credentials = Net.CredentialCache.DefaultCredentials

'Prepares to probe the link
Dim Res As Net.HttpWebResponse = Nothing
Dim ValidLink As Boolean
Try
'Tries to get the an ok response from the server
Res = CType(Req.GetResponse(), Net.HttpWebResponse)
ValidLink = True
Catch Ex As Net.WebException
Finally
If Res IsNot Nothing Then Res.Close()
End Try
</code>

I guess you'll have no trouble following the code. What this code does
is to set a Boolean flag (ValidLink) if the link is valid or not. It
won't be valid if the server can't be located or if the server returns
an error code (which will be the case if the file is not there).

Now, if what you really want is to *download* the link's content, then
an easier solution would be to use the System.Net.WebClient class:

<code>
'Again, this must be at the file level:
Imports Net = System.Net
'...
'...
'...
'This would be in a method:
Dim Link As String = "http://www.example.com/video.avi"
Dim Target As String = "C:\video.avi"

Dim Downloaded As Boolean
Dim W As New Net.WebClient
Try
W.DownloadFile(Link, Target)
Downloaded = True
Catch Ex As Net.WebException
End Try
</code>

The code above will download the file returned by the specified link
and save its contents in the "C:\video.avi" file (a terrible choice of
location, I admit, but this is just an example. I believe you'd put
the download in a more apropriate folder). If the link is valid and
the file was downloaded, the "Downloaded" flag will be set to True.

HTH.

Regards,

Branco.

Feb 1 '07 #6
Thank you Branco.
That worked perfectly, now I just gotta get the easy parts done!
BTW, thank you also for the other one (to actually download) even though
i wont use it right now, I know i will need it later.

Thank you.

<code>
'This must be at the file level:
Imports Net = System.Net
'...
'...
'...
'This would be in a method:
' Initializes the request
Dim Link As String = "http://www.example.com/video.avi"
Dim Req As Net.HttpWebRequest = _
CType(Net.WebRequest.Create(Link), Net.HttpWebRequest)
Req.Credentials = Net.CredentialCache.DefaultCredentials

'Prepares to probe the link
Dim Res As Net.HttpWebResponse = Nothing
Dim ValidLink As Boolean
Try
'Tries to get the an ok response from the server
Res = CType(Req.GetResponse(), Net.HttpWebResponse)
ValidLink = True
Catch Ex As Net.WebException
Finally
If Res IsNot Nothing Then Res.Close()
End Try
</code>
Feb 1 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tongu? Yumruk | last post: by
67 posts views Thread by Steven T. Hatton | last post: by
5 posts views Thread by Timo | last post: by
30 posts views Thread by Michael B Allen | last post: by
6 posts views Thread by RoSsIaCrIiLoIA | last post: by
19 posts views Thread by jeremyz | last post: by
11 posts views Thread by Bryan Crouse | last post: by
7 posts views Thread by polas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.