HowTo, Download first 1024 bytes of file ONLY?

N

NutsAboutVB

Basically, the title explains it.

I want to be able to read the first n bytes (say, 1024 bytes) of any
file (text or binary) off the internet in my vb.net application. I
specifically do not want to download the whole file (which in most
cases will be significantly large compared to the portion i do want).

I've looked into system.net.webclients only to find that the
OpenReadAsync methods doesn't even have a ProgressChanged event, and
I've looked into DownloadDataAsync method only to find that there is
a DownloadProgressChanged event but it doesn't actually pass the
buffered data to anything (and so you can't grab the start of the data
and then cancel the remaining transfer). There is a
DownloadDataCompleted event which is fired if you cancel the transfer
prematurely, however, in cases where the transfer is cancelled or there
is a transfer error, NON of the data transfered is actually passed to
this function either (transfer must complete for you to be able to
access the data through the DownloadDataCompletedEventArgs.Result
argument).

This is really disappointing, you'd figure these issues would be
clearly documented (but they are not and it really is guess work +
trial & error with ms these days - still). If they can't document such
a comprehensive framework sufficiently, maybe they shouldn't provide
101 classes to download files where not one can be rendered into
downloading a partial file.

Ok, I've had my moment of publicly whining (this is what happens
after 3-full days of performing errant google searches).

Could anyone point me in the right direction? I'm sure it would be a
generally useful post since there is a lack of discussion of this
anywhere on the internet. Something like this could also be useful for
resumable downloads, resumable downloading especially in the area of
creating an application that would support this.

Kindly,

Ethan
 
J

John Daragon

NutsAboutVB said:
Basically, the title explains it.

I want to be able to read the first n bytes (say, 1024 bytes) of any
file (text or binary) off the internet in my vb.net application. I
specifically do not want to download the whole file (which in most
cases will be significantly large compared to the portion i do want).

Well, I'm no specialist (I started programming C# err... yesterday.

But, how about something like this horrible non-compilable code snippet
(most of which was stolen from a Microsoft C# socket example) :

int port = PORT_NUMBER;
string server = SERVER_NAME;

Socket s = null;
IPHostEntry hostEntry = null;
hostEntry = Dns.GetHostEntry(server);

foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket = new Socket(ipe.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);

try
{

tempSocket.Connect(ipe);

}
catch { }
finally
{
}

if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
// explain to the user why we can't service the request,
// and either try again or abend.

DialogResult dr = MessageBox.Show(

"We don't appear to be able to connect
to the server. Is it running ?", "Socket Error",
MessageBoxButtons.RetryCancel,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);


continue;
}

}

// Send request to the server.

s.Send(Request, Request.Length, 0);

int bytes = 0;
i = 0;
// The following will block until the data is transmitted.
for (; ; )
{

// This returns a stream of bytes
NumberOfBytes = s.Receive(buffer, buffer.Length, 0);
string blah = Encoding.ASCII.GetString(buffer, 0,

NumberOfBytes);

// Add logic so you know when to stop
//
}

s.Close();
Ok, I've had my moment of publicly whining (this is what happens
after 3-full days of performing errant google searches).

IFYP. I've got this spiffy MSDN Universal thing, and I'd be dead in the
water without Google.

jd
 
S

SStory

John,

This shouldn't be to hard, the webrequest/webresponse objects (or
derivitives) that allow downloading a file from the internet can do so in
chunks (such as 1024).

I am doing that for downloading updates for my app.
 
N

NutsAboutVB

Hello,

I was hoping to not hear the words 'socket programming', since it does
remind me of the tedious hit and miss days of socket hackery back in
VB6 times - wasn't my fav. thing to work with, but I am sure sockets in
..net 2.0 is much more user friendly. Thank you for the code.

Regarding webrequest/webresponse, are you sure about this? Since when i
did look into the documentation i didn't read anything about this.

Excuse the delay in my response (it was Formula 1 weekend in melbourne
australia, and as you probably know, getting back into 'programmer
mode' does take some time).

:)
 
S

SStory

I don't have the code with me at this location, but yes I am sure that I am
getting it in 1024 chunks and then doing as I wish with it.

webrequest/response I think are abstract classes--could be wrong....seems
there was an httpwebresponse object or request object that I was actually
using.

A quick search produced this:
http://www.codeguru.com/Csharp/Csharp/cs_network/internetweb/article.php/c7005/

Maybe it will help you.

I am not certain but I think you get the stream and read it little by little
and just by having that stream, doesn't mean it has all been downloaded, so
I assume you could get 1024 bytes from it and then abort some how.

HTH,
Shane
 

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

Top