473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
Nov 18 '05 #1
2 2815
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
Nov 18 '05 #2
Dave wrote:
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,

--
Joerg Jooss
jo*********@gmx.net
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
2
by: fabien | last post by:
Hi, I am writing a POV-RAY editor with Python using either QT or GTK as GUI 'wrapper'. ( I am still trying both ) * * * * PYGTK * * * * I have downloaded PygtkScintilla-1.99.5. There is a...
2
by: John Grogan | last post by:
I have absolutely no experience in Javascript although am a programmer by trade. I have a problem in a third-party system, as follows. The system uses "web" forms to capture and save data. ...
8
by: Lloyd Sheen | last post by:
I have a problem with persisting the state of a control. It has several properties that can be determined but there is no event to indicate when the property changes. A postback occurs when one...
13
by: DavidS | last post by:
I have HTML web page with <asp:Image id=img_L runat=server ImageAlign=Top Visible=True Width=y Height=x></asp:Image>. For some images, less than 128K I can view image. Other image files > 128K,...
37
by: John Salerno | last post by:
I contacted my domain host about how Python is implemented on their server, and got this response: ------------------- Hello John, Please be informed that the implementation of python in our...
2
by: hzgt9b | last post by:
I've written a simple javascript page that parses an XML file... (Actually I just modified the "Parsing an XML File" sample from http://www.w3schools.com/dom/dom_parser.asp) The page works great...
18
by: Scott | last post by:
Hi, a problem with this following code is really bugging me. tform = fopen(country, "r"); fseek(tform, 9L, SEEK_SET); fgets(player2, 38, tform); printf("Player Name (save): %s", player);...
1
by: taskon2 | last post by:
Hi all I am trying to implement a save load class for a text based adventure game.... A save file must contain 3 arraylists that each contain user defined objects like the players inventory...
6
by: tinman77 | last post by:
Hello, I'm having a terrible time using the functions finfo_open and finfo_file. I'm using PHP 5 on IIS 5.1 and Windows XP. I have enabled php_mime_magic.dll and php_fileinfo.dll and also added...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.