Anantkumar wrote:
woudl the folloing work?No, I wanted to send it as entity(payload) in Request, but any good solution which satisfy my requirement I will do that. --Anant-- PostMethod
method = new PostMethod(url);
method.setDoAuthentication( true );
String helloWorld = new String(new Base64().encode("Hello,
World!".getBytes()));
XMLDocument xmlDocument = new XMLDocument().addElement(new
XML("mail")
..addElement(new XML("documents")
..addElement(new XML("document")
..addElement(new XML("type").addElement("txt"))
..addElement(new XML("content").addElement(helloWorld))
)
)
..addElement(new XML("addressees")
..addElement(new XML("addressee")
..addElement(new XML("name").addElement("Christopher
Robin"))
..addElement(new XML("address").addElement("1234 Main St."))
..addElement(new XML("city").addElement("Kenosha"))
..addElement(new XML("state").addElement("WI"))
..addElement(new XML("postal-code").addElement("53140"))
)
)
);
RequestEntity entity = new StringRequestEntity(xmlDocument.toString());
method.setRequestEntity(entity);
try {
int result = client.executeMethod(method);
System.out.println("Response status code: " + result);
if(result == 422) {
InputStream responseBody = method.getResponseBodyAsStream();
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(responseBody);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList errors = (NodeList)xpath.evaluate("/errors/error", document,
XPathConstants.NODESET);
for(int i = 0; i < errors.getLength(); i++) {
Node error = errors.item(i);
if(error.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(error.getTextContent());
}
}
} else if(result == 201)
{
System.out.println(method.getResponseHeader("Location"));
}
} finally {
method.releaseConnection();
}
} |
- Sending XML From client to service through HTTP GET Anantkumar
- Re: Sending XML From client to service through HTTP G... Stephan Koops
- Re: Sending XML From client to service through HT... Anantkumar
- Re: Sending XML From client to service throug... Stephan Koops
- Re: Sending XML From client to service throug... Kamal
- Re: Sending XML From client to service through HTTP G... Thierry Boileau
- Re: Sending XML From client to service through HTTP G... Leshek