Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: LWP: Downloading First 2KB of an HTML File

by Aristotle (Chancellor)
on Feb 22, 2003 at 00:17 UTC ( #237637=note: print w/replies, xml ) Need Help??

in reply to LWP: Downloading First 2KB of an HTML File

Of course, a Range header will fail in a lot of cases, where the webserver doesn't know ahead of time the size of the document it is going to transmit.

In that case you can use the $ua->request($request, \&callback, 4096); form of the request method. LWP::UserAgent will then call your callback function as it downloads, passing it chunks of the specified length.

Now the POD to LWP::UserAgent says this:

The request can be aborted by calling die in the callback routine. The die message will be available as the "X-Died" special response header field.

Obviously, the easiest route in this case is to request chunks of 2kb, and unconditionally die whenever the callback is called. That will do exactly what you want: load 2kb, then abort the request.

my $data; $ua->request($request, sub { $data = shift; die }, 2048);

There you go.

Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://237637]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2019-10-23 16:50 GMT
Find Nodes?
    Voting Booth?