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

D

Daniel Padron

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)
 
B

Bob O`Bob

Daniel said:
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
--
 
K

Karl E. Peterson

Daniel Padron said:
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
 
D

Daniel Padron

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.
 
B

Branco Medeiros

Daniel Padron wrote:
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.
 
D

Daniel Padron

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top