Newbie...Posting XML to Servlet

Discussion in 'Java' started by Aparna, Oct 10, 2005.

  1. Aparna

    Aparna Guest

    I am trying to send an XML to a SERVLET using the POST method and I run
    into a problem.

    I am trying to integrate a login component written by another team.
    They have a test client which shows the XML that will be sent to the
    servlet. Their input xml and mine matches exactly.
    I know that I am forming the input XML correctly, because if I go to
    http://app.com/test.jsp and replace the XML in the textbox with the XML
    I create in my code, it always gives me back a good response.

    I am using standard POST request in my code, nothing special, so I am
    lost as to why this happens. Am I missing something here? Any help is
    greatly appreciated.

    input xml from my code
    <?xml version="1.0" encoding="UTF-8"?>
    <Request>
    <Credentials>
    <UserID>smhsrvuser1</UserID>
    <Password>{SSHA}CoM8yMtxnTy8BR2PmOjkbe/c2vccHF+3w==</Password>
    </Credentials>
    <Data>
    <Action>23</Action>
    <ESELoginID>vrangaraj</ESELoginID>
    <AppID>1,2</AppID>
    </Data>
    </Request>

    correct output xml that I get back from the test.jsp
    <?xml version="1.0" encoding="UTF-8" ?>
    <SM>
    <STAT>
    <CD>0</CD>
    <TXT>OK</TXT>
    </STAT>
    <R>2^3^112</R>
    <G>
    <A2>8001^8100^8102^1111111111^8112^8110^8107^8105</A2>
    </G>
    </SM>

    incorrect output xml that I get back in the code
    <?xml version="1.0" encoding="UTF-8"?>
    <SM>
    <STAT>
    <CD>999</CD>
    <TXT>Invalid input xml</TXT>
    </STAT>
    </SM>
    ----------------------------------------
    code that I use to send the request


    code:
    --------------------------------------------------------------------------------

    //forming the document
    Document doc=null;
    String inputxml = null;
    try {
    doc = DocumentHelper.parseText("<?xml version=\"1.0\"
    encoding=\"UTF-8\"?><Request></Request>");
    }
    catch (DocumentException e)
    { e.printStackTrace(); }
    //forming the xml
    Element credElem = doc.getRootElement().addElement("Credentials");
    Element usrElem = credElem.addElement("UserID");
    if(usrElem!=null)
    usrElem.setText("smhsrvuser1");
    Element pswElem = credElem.addElement("Password");
    if(pswElem!=null)
    pswElem.setText("{SSHA}PmOjkbe/c2vccHF+3w==");
    Element datElem = doc.getRootElement().addElement("Data");
    Element actElem = datElem.addElement("Action");
    actElem.setText("23");
    Element logElem = datElem.addElement("ESELoginID");
    logElem.setText("marc");
    Element appElem = datElem.addElement("AppID");
    appElem.setText("1,2");
    inputxml = doc.asXML();
    //sending request to the servlet
    serviceURL="http://app.com/Login";
    URL url = new URL(serviceURL);
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

    wr.write(inputxml);
    wr.flush();
    // Get the response
    BufferedReader rd = new BufferedReader(new
    InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null)
    { retVal = retVal + line; }
    wr.close();
    rd.close(); }
    catch (Exception ex) { }
    //this always gives me the Invalid Input XML error
    System.out.println("\n"+retVal);
    Aparna, Oct 10, 2005
    #1
    1. Advertising

  2. Aparna

    HalcyonWild Guest

    Aparna wrote:
    > I am trying to send an XML to a SERVLET using the POST method and I run
    > into a problem.
    >
    >
    > I am using standard POST request in my code, nothing special, so I am
    > lost as to why this happens. Am I missing something here? Any help is
    > greatly appreciated.
    >
    > //forming the document
    > Document doc=null;
    > String inputxml = null;
    > try {
    > doc = DocumentHelper.parseText("<?xml version=\"1.0\"
    > encoding=\"UTF-8\"?><Request></Request>");
    > }
    > catch (DocumentException e)
    > { e.printStackTrace(); }
    > //forming the xml
    > Element credElem = doc.getRootElement().addElement("Credentials");
    > Element usrElem = credElem.addElement("UserID");
    > if(usrElem!=null)
    > usrElem.setText("smhsrvuser1");
    > Element pswElem = credElem.addElement("Password");
    > if(pswElem!=null)
    > pswElem.setText("{SSHA}PmOjkbe/c2vccHF+3w==");
    > Element datElem = doc.getRootElement().addElement("Data");
    > Element actElem = datElem.addElement("Action");
    > actElem.setText("23");
    > Element logElem = datElem.addElement("ESELoginID");
    > logElem.setText("marc");
    > Element appElem = datElem.addElement("AppID");
    > appElem.setText("1,2");
    > inputxml = doc.asXML();
    > //sending request to the servlet
    > serviceURL="http://app.com/Login";
    > URL url = new URL(serviceURL);
    > URLConnection conn = url.openConnection();
    > conn.setDoOutput(true);
    > OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    >
    > wr.write(inputxml);
    > wr.flush();
    > // Get the response
    > BufferedReader rd = new BufferedReader(new
    > InputStreamReader(conn.getInputStream()));
    > String line;
    > while ((line = rd.readLine()) != null)
    > { retVal = retVal + line; }
    > wr.close();
    > rd.close(); }
    > catch (Exception ex) { }
    > //this always gives me the Invalid Input XML error
    > System.out.println("\n"+retVal);



    Can you post at which line the exception is thrown. YOu are catching
    Exception e and that just about catches everything that is thrown its
    way.

    Also, are you using some other DOM parsing framework, apart from
    org.w3c.dom.* classes. Looks like JDOM to me.
    HalcyonWild, Oct 10, 2005
    #2
    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. alanb
    Replies:
    2
    Views:
    525
    alanb
    Apr 23, 2004
  2. Matthias Krueger
    Replies:
    3
    Views:
    29,152
    Matthias Krueger
    Sep 8, 2003
  3. ppcdev
    Replies:
    0
    Views:
    490
    ppcdev
    Jan 13, 2004
  4. circuit_breaker
    Replies:
    2
    Views:
    1,996
    Jack Jia
    Apr 4, 2004
  5. AmitKu
    Replies:
    7
    Views:
    501
    John Timney \(MVP\)
    Jan 8, 2007
Loading...

Share This Page