Newbie...Posting XML to Servlet

A

Aparna

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);
 
H

HalcyonWild

Aparna said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top