Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

LWP HTTPS and a sore head

by anchylo (Initiate)
on Jul 15, 2009 at 20:02 UTC ( [id://780458]=perlquestion: print w/replies, xml ) Need Help??

anchylo has asked for the wisdom of the Perl Monks concerning the following question:

Hello I am having difficulty downloading a file (and am tired of downloading them directly...
<code> #!/usr/bin/perl
use LWP;
my $url = 'https://192.168.1.1/cgi-bin/get-sae-log.cgi?host=thingy&date=20090703'; # Yes, HTTPS! my $browser = LWP::UserAgent->new;
my $response = $browser->get($url);
die "Error at $url\n ", $response->status_line, "\n Aborting" unless $response->is_success; print "Whee, it worked! I got that ", $response->content_type, " document!\n"; <\code>
results with:
C:\dhcp.logs\bc>xplore_lwp.pl
Whee, it worked! I got that application/x-bzip2 document!

ok, so i can see the document, but am at a loss as to how to name and download the file.

I have tried lwp-download, but got massive errors... any help appreciated.

Replies are listed 'Best First'.
Re: LWP HTTPS and a sore head
by Anonymous Monk on Jul 15, 2009 at 20:31 UTC
    I have tried lwp-download, but got massive errors...

    What errors?

    LWP::Simple

    use LWP::Simple ; if (mirror("http://www.sn.no/", "foo") == RC_NOT_MODIFIED) { ... }
      If you afraid of pasting your errors here :)
      try Mechanize.pm --> "http://search.cpan.org/~petdance/WWW-Mechanize-1.58/lib/WWW/Mechanize.pm"
Re: LWP HTTPS and a sore head
by tokpela (Chaplain) on Jul 16, 2009 at 08:37 UTC

    Use LWP::Simple and getstore which saves directly to a file.

    Untested code below...

    use strict; use LWP::Simple; my $url = 'https://192.168.1.1/cgi-bin/get-sae-log.cgi?host=thingy&da +te=20090703'; my $file = 'document.bzip2'; my $rc = getstore($url, $file); if (is_success($rc)) { "Success - saved [$url] to [$file]\n"; } else { "Error - [$rc] error in getting [$url]\n"; }
Re: LWP HTTPS and a sore head
by hnd (Scribe) on Jul 16, 2009 at 04:48 UTC
    if you are on *nix then you can pipe your data into a file like
    > (your program)>>(file name)
    but delete the print statement before the actual output.....
    =====================================================
    i'am worst at what do best and for this gift i fell blessed...
    i found it hard it's hard to find well whatever
    NEVERMIND
Re: LWP HTTPS and a sore head
by hnd (Scribe) on Jul 16, 2009 at 04:48 UTC
    if you are on *nix then you can redirect your data into a file like
    > (your program)>>(file name)
    but delete the print statement before the actual output.....
    =====================================================
    i'am worst at what do best and for this gift i fell blessed...
    i found it hard it's hard to find well whatever
    NEVERMIND
Re: LWP HTTPS and a sore head
by BaldManTom (Friar) on Jul 16, 2009 at 13:04 UTC
    Hi anchylo, I believe you've already "downloaded" the file, you just need to give it a filename and put it somewhere. Use any filename that makes sense to you:
    # Following your original code snippet my $filename = 'logfile.bz2'; open ( OF, '>', $filename ) || die "Can't open $filename locally"; print OF $response->content; close OF
    Note that you may need to binmode your filehandle before writing to it if your content isn't text.
      You may want to print $response->decoded_content to deal with the situation whereby the file is gzip encoded..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://780458]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-18 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found