I am trying to use LWP to simulate the submission of a form. However, during the submission of the form, I also want to simulate as if the browser has some cookie set already set. So far I tried a ton of things and nothing really works except this other hack I did which I don't like. If someone has a better solution that they can share, I would really appreciated it.
---- This is what I tried and doesn't work ----- use LWP::UserAgent; use HTML::Form; use LWP::Debug qw(+); use HTTP::Cookies; use Data::Dumper; # This site if redirected from another login page where the user # inputs a specific user/password or if the user and password is passe +d in , a specific cookie is sets # Assessing this page directly will have a default cookie set $site = "http://some_site_with_a_form.com"; # Creating the LWP user agent $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0"); # Creating cookie container $cookie_jar = HTTP::Cookies->new(); $cookie_jar->set_cookie(1, "foo", "bar", "/", "http://some_site_with_a +_form.com", "9000", 0, 0, 1000, 0); # tying the cookie container with the user agent $ua->cookie_jar($cookie_jar); # get the form from this site @forms = $ua->post($site); $form1 = $forms[0]; # only interested in the first form # and I can do something here to replace some values # from the form 1 but not needed for what I'm trying to show # $ua->add_cookie_header($form1.click) my $res = $ua->request($form1.click); # shows the cookie is set to default, which tells me # whatever I tried to do is useless print Dumper($res); -- It seems to work if I go into the site with the -- userid/password first which then sets the correct -- cookie use LWP::UserAgent; use HTML::Form; use LWP::Debug qw(+); use HTTP::Cookies; use Data::Dumper; # This site if redirected from another login page where the user # inputs a specific user/password or if the user and password is passe +d in , a specific cookie is sets # Assessing this page directly will have a default cookie set $site = "http://some_site_with_a_form.com"; # Creating the LWP user agent $ua = LWP::UserAgent->new; $ua->agent("Mozilla/5.0"); # Creating cookie container $cookie_jar = HTTP::Cookies->new(); # not needing this, just create an empty cookie container # $cookie_jar->set_cookie(1, "foo", "bar", "/", # "http://some_site_wi +th_a_form.com", "9000", 0, 0, 1000, 0); # tying the cookie container with the user agent $ua->cookie_jar($cookie_jar); # What I have to do, pretending to go into the page with # with a password. But one can see this is lame and # very bad form to expose password $res = $ua->get($site . "?user=admin&password=adminpasswd"); ## @forms = $ua->post($site); $form1 = $forms[0]; # only interested in the first form # and I do something to replace some values from the form 1 # $ua->add_cookie_header($form1.click) my $res = $ua->request($form1.click); # shows the cookie is set for the specific user "admin print Dumper($res);

In reply to LWP Cookies with HTML::Form by tvm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.