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


In reply to pass user name & password by gr8day

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.