Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
"be consistent"
 
PerlMonks  

http get with perl, help needed

by perlnewb123 (Sexton)
on Oct 16, 2008 at 20:14 UTC ( #717574=perlquestion: print w/ replies, xml ) Need Help??
perlnewb123 has asked for the wisdom of the Perl Monks concerning the following question:

Used perl for a while, just never tried it for something like this. Could someone show me an example of how to use perl with a HTTP Get? I see a ton of examples out there, just wondering how my request/response could fit into one. An example of my request/response, how would I format this into a script? Use LWP?
Request: GET /ws/ws.asmx/DecryptText?text=string&key=string HTTP/1.1 Host: test.test.com Response: HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="test.test.com/ws/test/">string</string>

Comment on http get with perl, help needed
Download Code
Re: http get with perl, help needed
by ikegami (Pope) on Oct 16, 2008 at 20:28 UTC

    Yes, I would.

    use strict; use warnings; use LWP::UserAgent qw( ); use URI::Escape qw( uri_escape ); my $ua = LWP::UserAgent->new(); my $response = $ua->get(sprintf( 'http://test.test.com/ws/ws.asmx/DecryptText?text=%s&key=%s', uri_escape($text), uri_escape($key), )); if (!$response->is_success()) { die($response->status_line(), "\n"); } binmode STDOUT; print $response->content();

    Update: s/\$ua->request/\$ua->get/ as per reply.

      Thank you!. What would I use for the request though? Assuming I do this:
      use strict; use warnings; use LWP::UserAgent qw( ); use URI::Escape qw( uri_escape ); my $ua = LWP::UserAgent->new(); my $response = $ua->request(sprintf( 'http://test.test.com/ws/ws.asmx/DecryptText?text=%s&key=%s', uri_escape('SomeText'), uri_escape('SomeDecryptionKey'), )); if (!$response->is_success()) { die($response->status_line(), "\n"); } binmode STDOUT; print $response->content();
      I get an error back:
      You need a request object, not 'http://test.test.com/ws/ws.asmx/Decryp +tText?text=SomeText&key=SomeDecryptionKey' at test.pl line 10
        Oops, that should be $ua->get(...) rather $ua->request(...)
Re: http get with perl, help needed
by JavaFan (Canon) on Oct 16, 2008 at 22:17 UTC
    My two favourite methods are LWP::Simple and `wget`. For instance:
    use LWP::Simple; my $document = get("http://www.perlmonks.org");
    or
    my $document = `wget -qO- http://www.perlmonks.org`;
    Most of the time I use LWP::Simple, but sometimes I try to download something from a site for which LWP::Simple doesn't fetch the page. It's usually much easier to use `wget` than to debug why I'm not able to fetch the page with LWP.

    If I need something more complex than a few simple downloads, I use WWW::Mechanize or LWP.

      How would you supply creds(auth) with this?
        I'd use either LWP::UserAgent, and define a get_basic_credentials method; use LWP::Simple and call $ua->credentials(); read the manual page of wget and detect it has --http-user and --http-passwd options, and it also willing to read a username/password from configuration files; or use WWW::Mechanize and call credentials().
Re: http get with perl, help needed
by perlnewb123 (Sexton) on Oct 17, 2008 at 16:03 UTC
    Ok, im getting close, adding the debug to LWP, it is now saying I need a proxy. Not sure how to set that up, but looking. LWP::UserAgent::_need_proxy: Not proxied
Re: http get with perl, help needed
by perlnewb123 (Sexton) on Oct 20, 2008 at 18:59 UTC
    This was fixed with calling Authen::NTLM; Thank you everyone.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-04-20 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (488 votes), past polls