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

I need to pass a required login.username and login.password parameters with an XML message. I was given an example in Java, but not being a java junkie, I'd rather do it in perl. can anyone recommend a suitable perl substitue code? What I've written using
use LWP::UserAgent; use HTTP::Request::Common;
sends a file, but I get an error message that I'm not authenticated.

the java code:
public class HttpPost { private static final String username = "ccxml"; private static final String password = "ccxml"; protected String fileName = null; protected URL url = null; public static void main(String[] args) throws Exception { HttpPost http = new HttpPost(args[0], args[1]); System.out.println(http.post()); } public HttpPost(String url, String fileName) throws MalformedURLEx +ception { this.fileName = fileName; this.url = new URL(url); } public String post() throws Exception { // read the file String message = null; if (fileName != null) { FileInputStream fis = new FileInputStream(fileName); int x= fis.available(); byte b[]= new byte[x]; fis.read(b); message = new String(b); } StringBuffer buf = new StringBuffer(); Map args = new HashMap(); if (message != null) { args.put("message", message); } args.put("login.username", username); args.put("login.password", password); String arguments = this.urlEncodeArgs(args); URLConnection con = url.openConnection(); con.setDoOutput(true); con.setUseCaches(false); if ((con instanceof HttpURLConnection)) { ((HttpURLConnection) con).setInstanceFollowRedirects(true) +; } DataOutputStream out = new DataOutputStream(con.getOutputStrea +m()); out.writeBytes(arguments); out.flush(); out.close(); InputStream in = con.getInputStream(); BufferedReader post = new BufferedReader(new InputStreamReader +(in)); String line = new String(); while ((line = post.readLine()) != null) { buf.append(line); buf.append("\n"); } return buf.toString(); } /** URL Encodes a Map of arguements */ private String urlEncodeArgs(Map args) { StringBuffer buf = new StringBuffer(); if (args != null) { Iterator i = args.entrySet().iterator(); while (i.hasNext()) { Map.Entry entry = (Map.Entry) i.next(); String name = (String) entry.getKey(); Object value = entry.getValue(); String valueStr = null; if (name != null && value != null) { if (value instanceof String) { valueStr = (String) value; } else { valueStr = value.toString(); } if (valueStr != null && valueStr.length() > 0) { if (buf.length() > 0) buf.append("&"); try { buf.append(URLEncoder.encode(name, "UTF-8" +)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } buf.append('='); try { buf.append(URLEncoder.encode(valueStr, "UT +F-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } } return buf.toString(); } }

Edited by planetscape - added readmore tags

Replies are listed 'Best First'.
Re: pass user name & password
by kwaping (Priest) on Feb 18, 2006 at 00:02 UTC
Re: pass user name & password
by idsfa (Vicar) on Feb 18, 2006 at 20:58 UTC

    Your java code boils down to POSTing an XML document to a URL that requires authentication. As written the XML document appears to come from STDIN. You can duplicate this functionality with lwp-request, which comes with the LWP package (sometimes under the name of POST). You could use this like:

    lwp-request -m POST -C username:password

    Alternately, you could post your perl code, so that we could help you with it ...


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon