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

Hi All, This following works in Linux Bash script:
wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u80-b15/jre-7u80-windows- +i586.exe
If you don't get it right, you download something that tells you to click the "accept License" button. Curl always gets the "accept license" button note. So I switched to wget. I have read numerous responses to LWP::UserAgent, but I am confused as to how to handle the cookie. Is it necessary that this be done with a file? Can it be done directly without a file? Would some kind person mind showing me an example of the above? Many thanks, -T

Replies are listed 'Best First'.
Re: How do I convert this wget to perl?
by atcroft (Abbot) on Oct 03, 2015 at 21:15 UTC

    With LWP::UserAgent, you can specify a "cookie jar" file by doing the following:

    # Assumes $ua is an LWP::UserAgent object use HTTP::Cookies; my $cookie_jar = HTTP::Cookies->new( file => "./lwp_cookies.dat", autosave => 1, ); $ua->cookie_jar($cookie_jar);

    You can then use the $cookie_jar->add_cookie_header() method to potentially pre-load the cookie jar with the value. You may also be able to do so instead by using the additional arguments of the $lwp->get() method, similar to the following (untested):
    $ua->get( $url, ':content_file' => 'file.bin', 'Cookie' => '...', );

    Hope that helps.

      Sorry for being slow here, but I am having trouble wrapping my mind around what is going on with

      $ua->get( $url, ':content_file' => 'file.bin', 'Cookie' => '...', );

      Do I create 'file.bin'?

      or does the 'get' do that?

      What goes in the '...' part of the 'Cookie'?

      Does this download the cookie for me?

      And I guess I do not understand what a cookie is. I thought it was "oraclelicense=accept-securebackup-cookie" or is that the name of something else that has to be downloaded?

      Do I do a second 'get' to download the .exe file after the above? or does the above do the whole thing?

        See LWP::UserAgent. The ->get method sends a GET request and downloads the response to the file file.bin as per the :content_file argument.

        I don't find a special Cookie argument documented for ->get though, so I suspect that this won't work to set the cookie header as you want it. I think you will have to use HTTP::Cookies to set the cookie to the value you want.

Re: How do I convert this wget to perl?
by Corion (Patriarch) on Oct 03, 2015 at 20:53 UTC