Posting Credentials using LWP To WebMethods

Discussion in 'Perl' started by mdh, Apr 20, 2004.

  1. mdh

    mdh Guest

    I have an application written for mod_perl and Apache that needs to be
    able to send some XML queries to a WebMethods server for access to
    corporate systems. I have been attempting to use LWP to POST my XML
    query to the target WebMethods server and find I only get 401s
    (authorization errors) from the WebMethods server when it is
    configured for username/password authentication. If credentials are
    turned off on the WM server, I can get my expected XML replies.
    Here's what I've tried:

    1) WORKS: my basic PERL code hitting a local Apache server
    (1.3.29 or 2.0.48) simulating WebMethods by sending
    canned XML replies and requiring a username/password
    credential in a known realm of "webMethods"
    2) WORKS: the same PERL code hitting a real WebMethods
    server that has authentication turned off
    3) WORKS: using a browser to surf to a second real WebMethods
    server with authentication turned on that requires:
    username: myuser
    password: mypasswd
    realm: webMethods
    4) BROKE: the same PERL code hitting that second WM server
    using the credentials in bullet #3 using base64 encoding
    of username and password

    When the admin of the WebMethods server looks at his logs, he sees the
    hit coming from LWP trying to perform the post as default/default (or
    no authentication). Seems to indicate LWP doesn't recognize something
    unique coming back from WebMethods when it wants credentials to be
    supplied for a realm. Obvious things I HAVE confirmed are:

    QUESTIONS:

    1) has anyone else ever gotten a PERL script to post XML to a
    WebMethods server with credentials using LWP?
    2) is there a special agent string I can use that WebMethods
    might like?
    3) WebMethods provides a client.jar class library to write
    Java scripts to access the server -- am I just going to
    have to invoke a Java wrapper script and solve the problem
    with more indirection (smile)?

    Any insight would be greatly appreciated. THANKS




    =========CODE SNIPPET BELOW==============

    # IMPORTANT - documentation for the LWP::UserAgent::credentials
    # function is slightly out of date regarding the net
    # location parameter (the first one) - it must
    # be of the form "host.domain.com:port" even if you
    # are using the default default 80 for http or 443
    # for https

    my $WEBMETHODSUSERID = 'myuser';
    my $WEBMETHODSPASSWORD = 'mypasswd';
    $WEBMETHODSUSERID = MIME::Base64::encode($WEBMETHODSUSERID);
    $WEBMETHODSPASSWORD = MIME::Base64::encode($WEBMETHODSPASSWORD);
    $WEBMETHODSUSERID =~ tr/\n//d; # strip trailing LF (???)
    $WEBMETHODSPASSWORD =~ tr/\n//d;

    my $WEBMETHODSREALM;
    my $WEBMETHODSNETLOCATION;
    my $WEBMETHODS_GETACCOUNTINFO_URL;

    $WEBMETHODS_GETACCOUNTINFO_URL =
    "http://192.168.29.41:5555/invoke/myServices.Transaction:CustomerLookup";
    $WEBMETHODSNETLOCATION = "192.168.29.41:5555";
    $WEBMETHODSREALM = "webMethods"; # has to match Realm name
    # displayed in browser prompt


    my $ua = LWP::UserAgent->new();
    $ua->credentials($WEBMETHODSNETLOCATION, $WEBMETHODSREALM,
    $WEBMETHODSUSERID, $WEBMETHODSPASSWORD);
    $ua->agent('Mozilla/5.0');
    my $req = HTTP::Request->new(POST => $WEBMETHODS_GETACCOUNTINFO_URL );
    $req->content_type('text/xml');
    $req->content($webmethodsQuery);
    my $response = $ua->request($req);

    #----------------------------------------------------------
    # upon return, we either have:
    # a) an error indicated by a response code other than 200
    # b) a reply from WebMethods with <Response>xxx</Response>
    # indicating success
    # c) a reply from WebMethods with no <Response> but with
    # <FailureReasonID> and other error tags
    #-----------------------------------------------------------
    if ($response->code() != 200) {
    %result = ( errorcode => $response->code(),
    errormsg => 'ERROR: unexpected reply from WebMethods
    in GetAccountInfo' );
    }
    else {
    # if we got a reply from WebMethods, it will either have
    <Response>xxx</Response> data
    # or the following XML values below the <MyAppMessage> level:
    # <FailureReasonID>ERROR OCCURRED</FailureReasonID>
    # <ErrorNumber>100</ErrorNumber>
    # <ErrorMessage>Timeout</ErrorMessage>
    my $entireXMLreply = $response->content(); # get the whole
    convoluted XML structure
     
    mdh, Apr 20, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. msnews.microsoft.com
    Replies:
    0
    Views:
    920
    msnews.microsoft.com
    May 10, 2006
  2. Peter
    Replies:
    2
    Views:
    99
    Grant Wagner
    Jun 2, 2004
  3. dan baker

    q about LWP:UserAgent credentials()

    dan baker, Jan 19, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    116
    dan baker
    Jan 20, 2004
  4. John Stumbles

    LWP::UserAgent credentials netloc and realm

    John Stumbles, May 27, 2005, in forum: Perl Misc
    Replies:
    0
    Views:
    111
    John Stumbles
    May 27, 2005
  5. Nalli

    LWP credentials help

    Nalli, Mar 15, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    209
    Peter Scott
    Mar 16, 2007
Loading...

Share This Page