Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by Aristotle (Chancellor)
on Feb 22, 2003 at 00:17 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found