473,626 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ByVa l sURL As String,ByVal sFileName As
String) As Boolean

Dim oRequest As System.Net.Http WebRequest =
System.Net.Http WebRequest.Crea te(sURL)
oRequest.UserAg ent = "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.WebR esponse =
oRequest.GetRes ponse()
Dim oReader As System.IO.Strea mReader = New
System.IO.Strea mReader(oRespon se.GetResponseS tream)

Try
Dim oFile As System.IO.FileS tream = New _
System.IO.FileS tream(sFileName ,
System.IO.FileM ode.OpenOrCreat e, System.IO.FileA ccess.ReadWrite ,
System.IO.FileS hare.None)
Dim oWriter As New System.IO.Strea mWriter(oFile)
Dim sText As String = oReader.ReadToE nd
oWriter.Write(s Text)
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 2829
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(ByVa l sURL As String, ByVal sFileName As
String) As Boolean

Dim oRequest As System.Net.Http WebRequest =
System.Net.Http WebRequest.Crea te(sURL)
Dim oResponse As System.Net.WebR esponse =
oRequest.GetRes ponse()
Dim oReader As System.IO.Binar yReader = New
System.IO.Binar yReader(oRespon se.GetResponseS tream)
Dim oFile As System.IO.FileS tream = New _
System.IO.FileS tream(sFileName ,
System.IO.FileM ode.OpenOrCreat e, System.IO.FileA ccess.ReadWrite ,
System.IO.FileS hare.None)
Dim oWriter As New System.IO.Binar yWriter(oFile)

Try
Do
oWriter.Write(o Reader.ReadByte )
Loop
Catch ex As System.IO.EndOf StreamException
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(ByVa l sURL As String, ByVal sFileName As
String) As Boolean

Dim oRequest As System.Net.Http WebRequest =
System.Net.Http WebRequest.Crea te(sURL)
Dim oResponse As System.Net.WebR esponse =
oRequest.GetRes ponse()
Dim oReader As System.IO.Binar yReader = New
System.IO.Binar yReader(oRespon se.GetResponseS tream)
Dim oFile As System.IO.FileS tream = New _
System.IO.FileS tream(sFileName ,
System.IO.FileM ode.OpenOrCreat e, System.IO.FileA ccess.ReadWrite ,
System.IO.FileS hare.None)
Dim oWriter As New System.IO.Binar yWriter(oFile)

Try
Do
oWriter.Write(o Reader.ReadByte )
Loop
Catch ex As System.IO.EndOf StreamException
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.CanRea d) {
throw new ArgumentExcepti on("Not readable", "source");
}

if (!target.CanWri te) {
throw new ArgumentExcepti on("Not writable", "target");
}

byte[] buffer = new byte[0x1000];
int bytes;
try {
while ((bytes = source.Read(buf fer, 0, buffer.Length)) > 0) {
target.Write(bu ffer, 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
3431
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 Pentagjetvedeh karuvificials madhla reachathe strategy in karkun campaign deshatinst terrorism. "mudivae maretu winning or losing karkun global varti jetvedeh terror?" Mr. Rumsfeld adugued in a recent memormariyuum. vede velli jetvedeh madhla...
2
3549
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 lexPOV.cxx file in the package, but I can not find any POV keywords in any file ( there are about 150 POV keywords). Did i miss it, and if not, how do I create one and include it for the library building ?
2
1666
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. When the form is called up it should "remember" some field contents from previous sessions. It works intermittently, other times produces "error on page" errors. I have narrowed it down to some lines in a Javascript script that is called when...
8
1257
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 of a array of buttons is clicked. On the click new information will be displayed but I need to save information from the control (which I cannot change) and reset it when the new information is displayed. Is there an article I can reference (I...
13
1337
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, image is not viewable - comes up with X in picture box. Is there size restriction. How can I remove this - or how can I show images > 128K in web page. NOTE: x & y in asp:Image spec are distinct values - say 128 x 128 or 64 x 64 image sizes
37
2468
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 server is through mod_python integration with the apache. These are the steps needed for you to be able to run .py script directly
2
2094
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 standalone... but when I try to make this work under frames I get "Error: Object required" when the following line executes: xmlDoc.getElementsByTagName("to"); The standalone file is named treeView.htm (attached). You should be
18
2085
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); printf("Player Name (form): %s", player2);
1
1145
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 arrayList, the position of all the characters and items in the rooms of the game world..... if i manage to write all the arraylists in one file - is this even possible... using append or smthing- then how could i separate them when i read back the file...
6
6495
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 the lines mime_magic.debug = Off and mime_magic.magicfile = "C:\Program Files\PHP\extras\magic.mime". Now my phpinfo() says fileinfo support is enabled and mime_magic support is enabled. However, I keep getting this error:
0
8262
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.