HTTPDownloader

Topics: Updater Application Block
Jan 23, 2004 at 1:59 PM
originally posted by: Estrada

I made an HTTPDownloader that works nice. I'm tired of BITSDownloader, it doesn't works ok on my iis, i dont know why. This is the main sub.


Friend Sub DownloadFiles()
Dim i As Integer
Dim fs As FileStream
Dim succeeded As Boolean
Try
' copy each file in the array
For i = 0 To _sourceFiles.Length - 1
succeeded = False
fs = New FileStream(_destFiles(i), FileMode.Create, FileAccess.Write)
request = HttpWebRequest.Create(sourceFiles(i))
_response = _request.GetResponse()

Dim length As Long = _response.ContentLength
If (length = -1 Or length > Int32.MaxValue) Then
length = Int32.MaxValue
End If
Dim buffer(Math.Min(DefaultCopyBufferLength, CInt(length))) As Byte
Dim s As Stream = _response.GetResponseStream()
Dim nread As Int32
Do
nread = s.Read(buffer, 0, CInt(buffer.Length))
fs.Write(buffer, 0, nread)
Loop While (nread <> 0)
If Not (fs Is Nothing) Then
fs.Close()
fs = Nothing
End If
s.Close()
_response.Close()
_request = Nothing
succeeded = True
Next i
Catch e As Exception
Throw New ApplicationException("Error copying files", e)
Exit Sub
Finally
If Not succeeded Then
'deletes the file if download fail
File.Delete(_destFiles(i))
End If
End Try
callbackFunction(jobID)
End Sub
Sep 27, 2004 at 11:24 PM
originally posted by: Moreira

what other changes did you made in the code? the app config's had any changes or they automatic detect the source of the download?
Oct 12, 2004 at 1:06 AM
originally posted by: Aurelian

where didi u put this piece of code?
Oct 12, 2004 at 4:51 AM
originally posted by: Estrada

Make a HTTPDownloader class, see the example at the documentacion about how to make it. Then Put this code on the DownloadFiles() sub. Dont forget to change the <downloader> tag at de app.config to use the HTTPDownloader class instead of the BITSDonloader class.
Oct 14, 2004 at 9:36 AM
originally posted by: Aurelian

What is DefaultCopyBufferLength variable. It is a constant?
Oct 14, 2004 at 9:51 AM
originally posted by: Estrada

Yes, it's a constant. I put it on 256.
Oct 14, 2004 at 10:05 AM
originally posted by: Aurelian

Done c# code here:

<code>

internal void DownloadFiles()
{


int DefaultCopyBufferLength = 512;
int _i = 0;
FileStream fs;
bool succeeded = false;
try
{
for (int i = 0; i <= _sourceFiles.Length - 1; i++)
{
succeeded = false;
fs = new FileStream(_destFilesi, FileMode.Create, FileAccess.Write);
WebRequest request = HttpWebRequest.Create(sourceFilesi);

WebResponse _response = _request.GetResponse();
long length = _response.ContentLength;
if ((length == -1 | length > Int32.MaxValue))
{
length = Int32.MaxValue;
}
byte[] buffer = new byteMath.Min(DefaultCopyBufferLength, System.Convert.ToInt32(length));
Stream s = _response.GetResponseStream();
Int32 nread;
do
{
nread = s.Read(buffer, 0, System.Convert.ToInt32(buffer.Length));
fs.Write(buffer, 0, nread);
} while ((nread != 0));
if (!((fs == null)))
{
fs.Close();
fs = null;
}
s.Close();
_response.Close();
_request = null;
succeeded = true;
_i = i;
}
}
catch (Exception e)
{
throw new ApplicationException("Error copying files", e);
}
finally
{
if (!(succeeded))
{
File.Delete(_destFiles_i);
}
}
callbackFunction(jobID);
}

</code>
Dec 14, 2004 at 9:51 AM
originally posted by: figuerres2

Hi there Folks, I am just starting to look at using the AUB and wonder why not use this to get the file:


Dim aWC As WebClient
SiteURI = New Uri(Web1)
SiteSP = ServicePointManager.FindServicePoint(SiteURI)
aWC = New WebClient
aWC.BaseAddress = SiteURI.ToString
aWC.DownloadFile(SiteURI.ToString & "FolderName/" & FIleName, SaveAsFilename)

am i missing something or woun't this be simpler??