Help: Trying to Load URL and save to File in Dot.Net

D

Dave

Hello. I need to load an URL and save it to a file in Asp.Net. The
function below is creating the file, but isn't putting the data in it.
Also the data is binary, so I'm not sure if I need to fiddle with
encoding or whatnot.
Dave

Private Function GetURLSave(ByVal sURL As String,ByVal sFileName As
String) As Boolean

Dim oRequest As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create(sURL)
oRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR
1.2.30703)"
Dim oResponse As System.Net.WebResponse =
oRequest.GetResponse()
Dim oReader As System.IO.StreamReader = New
System.IO.StreamReader(oResponse.GetResponseStream)

Try
Dim oFile As System.IO.FileStream = New _
System.IO.FileStream(sFileName,
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite,
System.IO.FileShare.None)
Dim oWriter As New System.IO.StreamWriter(oFile)
Dim sText As String = oReader.ReadToEnd
oWriter.Write(sText)
oWriter.Flush()
oWriter.Close()
oFile.Close()

Catch ex As Exception
GetURLSave = False
Finally
GetURLSave = True
End Try

oReader.Close()
oResponse.Close()

End Function
 
D

Dave

What I forgot to say was that I wanted this function to open an URL to
a BINARY file and save the BINARY file. The sample in my original post
works perfectly for text, such as HTML. Below is another version of
the same function that works with BINARY files, rewritten to use the
BinaryReader and BinaryWriter class and to read and write until it
hits the end of the stream. It works with PDF's, GIF's or JPG's:

Public Function GetURLSave(ByVal sURL As String, ByVal sFileName As
String) As Boolean

Dim oRequest As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create(sURL)
Dim oResponse As System.Net.WebResponse =
oRequest.GetResponse()
Dim oReader As System.IO.BinaryReader = New
System.IO.BinaryReader(oResponse.GetResponseStream)
Dim oFile As System.IO.FileStream = New _
System.IO.FileStream(sFileName,
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite,
System.IO.FileShare.None)
Dim oWriter As New System.IO.BinaryWriter(oFile)

Try
Do
oWriter.Write(oReader.ReadByte)
Loop
Catch ex As System.IO.EndOfStreamException
GetURLSave = True
End Try

oWriter.Close()
oWriter = Nothing
oFile.Close()
oFile = Nothing
oReader.Close()
oReader = Nothing
oResponse.Close()
oResponse = Nothing
oRequest = Nothing



End Function
 
J

Joerg Jooss

Dave said:
What I forgot to say was that I wanted this function to open an URL to
a BINARY file and save the BINARY file. The sample in my original post
works perfectly for text, such as HTML. Below is another version of
the same function that works with BINARY files, rewritten to use the
BinaryReader and BinaryWriter class and to read and write until it
hits the end of the stream. It works with PDF's, GIF's or JPG's:

Public Function GetURLSave(ByVal sURL As String, ByVal sFileName As
String) As Boolean

Dim oRequest As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create(sURL)
Dim oResponse As System.Net.WebResponse =
oRequest.GetResponse()
Dim oReader As System.IO.BinaryReader = New
System.IO.BinaryReader(oResponse.GetResponseStream)
Dim oFile As System.IO.FileStream = New _
System.IO.FileStream(sFileName,
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite,
System.IO.FileShare.None)
Dim oWriter As New System.IO.BinaryWriter(oFile)

Try
Do
oWriter.Write(oReader.ReadByte)
Loop
Catch ex As System.IO.EndOfStreamException
GetURLSave = True
End Try

oWriter.Close()
oWriter = Nothing
oFile.Close()
oFile = Nothing
oReader.Close()
oReader = Nothing
oResponse.Close()
oResponse = Nothing
oRequest = Nothing

It's not terribly efficient to write each byte individually. It also makes
little sense to use Readers and Writers when all you want to do is read and
write binary content "as is". This sample method copies contents from any
type of readable stream to any type of writable stream (in blocking mode).

public void Copy(Stream source, Stream target) {
if (!source.CanRead) {
throw new ArgumentException("Not readable", "source");
}

if (!target.CanWrite) {
throw new ArgumentException("Not writable", "target");
}

byte[] buffer = new byte[0x1000];
int bytes;
try {
while ((bytes = source.Read(buffer, 0, buffer.Length)) > 0) {
target.Write(buffer, 0, bytes);
}
}
finally {
target.Flush();
}
}

Cheers,
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top