"Sucking" .txt files from the Internet

H

Hrvoje Vrbanc

Hello all,

I'd like to use an ASP.NET web application to download (or stream down)
"third-party" TXT, HTML and XML files from the Internet and then save them
to local HDD. I encountered some problems when using streaming (URI formats
are not supported).

What's the best way to do it?

Thnak you in advance,
Hrvoje
 
S

S. Justin Gengo

'---Create the request
FileWebRequest = System.Net.WebRequest.Create(New Uri(CurrentLink))
FileWebRequest.Timeout = 2000
'---Get the response produced by the request
FileResponse = FileWebRequest.GetResponse
'---Download the page content into a stream
FileStream = FileResponse.GetResponseStream
FilePath = "C:\MyFile.txt"
WriteStream = New System.IO.FileStream(FilePath, IO.FileMode.CreateNew)
Dim Length As Int32 = 4096
Dim buffer(Length) As Byte
Dim BytesRead As Int32 = FileStream.Read(buffer, 0, Length)
While BytesRead > 0
WriteStream.Write(buffer, 0, BytesRead)
BytesRead = FileStream.Read(buffer, 0, Length)
End While
WriteStream.Close()
WriteStream = Nothing
FileStream.Close()
FileStream = Nothing


Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

Scott M.

FYI:

WriteStream = Nothing
FileStream = Nothing

Setting object variables = Nothing in .NET is only useful when you want the
object variable to fall off the stack before the end of the method. This is
rare and so setting object variables to nothing doesn't accomplish much for
you in .NET.
 
S

S. Justin Gengo

Scott,

I do so in my code because I wrote a scraper. The scraper spins a new thread
and grabs all the files it finds on the web page it's pointed at. Because
multiple files are downloaded in a loop it is necessary to set the streams
to nothing in order to re-use them in the loop.

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top