• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Trying to POST an XML to php based Web Service via HTTP POST method

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,

I am trying to POST an XML to php based Web Service via HTTP POST method. The php form of the target system is something like this :

[PHP]<?php

$xmlstring = '<?xml version="1.0" ?>';
$xmlstring .= '<data>';
$xmlstring .= '<ordernum>1234567890123</ordernum>';
$xmlstring .= '<custname>RUSDY AB. AZIZ</custname>';
$xmlstring .= '<mobilenum>0136130702</mobilenum>';
$xmlstring .= '<orderstatus>OnHold</orderstatus>';
$xmlstring .= '<prodtype>STX</prodtype>';
$xmlstring .= '</data>';

?>

<form name="icp2swans" method="POST" action="http://n9.intra.tm/swans/openapi/icpwaiters.php">
<!--- <form name="icp2swans" method="POST" action="icpwaiters.php"> --->
<!--- <input type="hidden" name="secretkey" value="ef6bf191c37ff1a78633dca0434ef147"> --->
<p>XML Text :</p> <!--- <input type="text" name="ordernum" value="123456789012345" size="15"><br> --->
<textarea name="xmldata" rows="10" cols="100"><?php echo $xmlstring; ?></textarea>
<p><input type="submit" value="Submit"></p>
</form>[/PHP]


My java code is that doe the POST action is something like this:


The XML that is to be sent is :

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<data>
<ordernum>1-J0K0Y</ordernum>
<custname>Ashwaq</custname>
<mobilenum>0125598693</mobilenum>
<orderstatus>Submitted</orderstatus>
<prodtype>DEL</prodtype>
</data>



The problem is that the programme does send the XML to the target system, however the target is returning a result that is indicates an error;

<?xml version="1.0" ?>
<data>
<errorcode>1</errorcode>
<errormsg>XML text was Empty. Sorry, you have submitted an empty form</errormsg>
</data>

According to the administrator of the target system, the error is due to the fact that the something in my HTTP message(the envelope must I presume) must conform to the name attribute of the textarea element in his php form. He even gave me a fragment of his code:



Input name: “xmldata”

This is the first time I am working with a code that requires me to interface with a php based system. I don't even know what I assumed above is correct in fact I don't know PHP. Please help. If any of the info is not clear please let me know.I will feedback ASAP. Really urgent. Thank you all in advance.
 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has nothing to do with PHP. Where in your Java code do you think you are setting the value of "xmldata"?
 
Robinson Francis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you MR. Moores,

The code I have used is the general code that is used to POST xml messages across the system in my company. However for this interface, the request is quite different. In fact this is the first time I am looking at the codes that deal with HttpPOST. Generally the xml that is to be sent is the inputMsg String object. This is later turned into a RequestEntity object @ line 90.

RequestEntity entity = new InputStreamRequestEntity(IOUtils.toInputStream(inputMsg), contentType);


That object is later set to the PostMethod object line 91

post.setRequestEntity(entity);

The final stage of sending the data to the form is done by line110:

int statusCode = client.executeMethod(post);

I am not sure whether I have correctly explained as most of it was learnt recently from other websites.

I have also tried to use a small code to do the POSTing, just to test out key concepts:

i

But the XML in the response still indicated an error.

Thank you.

 
Tim Moores
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"<?xml version=?>" - assuming that nothing got lost during posting, then this is an incorrect XML declaration.

Here's example code of how to POST using just the built-in classes: http://www.exampledepot.com/egs/java.net/Post.html
 
Robinson Francis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim, thanks for the speedy response.


I tried doing it the usual way,

<?xml version = "1.0" encoding = "utf-8"?>

But there would always be error thrown by the Compiler, I guess it has something to do with the special characters. I "". . I know that we have to use escape character and so I have modified the xmlData string to contain escape characters.

String xmlData = "<?xml version = \"1.0\" encoding = \"utf-8\"?>"+"<data>"+"<ordernum>1-J0K0Y</ordernum>"+"<custname>Ashwaq</custname>"+"<custname>Ashwaq</custname>"+"<mobilenum>0125598693</mobilenum>"+"<orderstatus>Submitted</orderstatus>"+"<prodtype>DEL</prodtype></data>";

Tried this as well but still the response is :



<?xml version="1.0" ?><data><errorcode>1</errorcode><errormsg>XML text was Empty. Sorry, you have submitted an empty form</errormsg></data>


I have to catch a train. See you later. Thanks for you help. Will login later tonight. Thanks again.
 
Greenhorn
Posts: 16
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robinson,

Check the code below which I wrote and I got response as "<html><head><title>504 Gateway Timeout</title></head><body><h1>Gateway Timeout</h1>
<p>Server error - server 208.73.210.29 is unreachable at this moment.<br><br>Please retry the request or contact your adminstrator.<br></p>
</body></html>"




Just see if this helps. I've used HttpClient-4.1.2.

Thanks.
 
Robinson Francis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Sorry for not replying to you all for a long time I managed to find the answer but only now did i have the time to post the solution to the problem. What I understand from this problem is that we are trying to write a programme that would mimic a standard HTML form that would POST data to server. As you would remember a HTML form will contain text areas, checkbox, radio button, SELECT tags and so on just like below:

<FORM ACTION="/cgi-bin/test" METHOD="POST">
Text: <INPUT NAME="text1">
<BR><INPUT type=checkbox NAME="ok" VALUE="yes">Ok?
<BR><SELECT NAME="aChoice">
<OPTION VALUE="one">1
<OPTION VALUE="two">2
</SELECT>
<BR><TEXTAREA NAME="comments" ROWS=10 COLS=80>
</TEXTAREA>
<BR>
</FORM>

In this case for my programme must mimic this form:

<form name="icp2swans" method="POST" action="http://n9.intra.tm/swans/openapi/icpwaiters.php">
<p>XML Text :</p>
<textarea name="xmldata" rows="10" cols="100"><?php echo $xmlstring; ?></textarea>
<p><input type="submit" value="Submit"></p>
</form>


The key is to mimic the textarea element. I tried to see how the for would be sending the data in the textarea element. To do that try opening the form in FireFox. and run HttpFox, and click submit.

You will find that the data sent out was :
xmldata=%3C%3Fxml+version%3D%271.0%27+encoding%3D%27utf-8%27%3F%3E%3Cdata%3E%3Cordernum%3E1-J0K0Y%3C%2Fordernum%3E%3Ccustname%3EAshwaq%3C%2Fcustname%3E%3Cmobilenum%3E0125598693%3C%2Fmobilenum%3E%3Corderstatus%3ESubmitted%3C%2Forderstatus%3E%3Cprodtype%3EDEL%3C%2Fprodtype%3E%3C%2Fdata%3E


By using

method.setRequestEntity(entity);

in my code :

the data that was sent out was :

"<?xml version="1.0" encoding="utf-8" standalone="no"?> <data> <ordernum>1-J0K0Y</ordernum><custname>Ashwaq</custname><mobilenum>0125598693</mobilenum><orderstatus>Submitted</orderstatus> <prodtype>DEL</prodtype> </data> "


So we have to create a name-value pair and send it as a data , the name being xmldata, value being the xml .

That was achieved via :

method.setParameter("xmldata", xmlData);


The data sent out was just like as it was sent out from the form.



The following lines were added :



As they would enable debugging the http message. This is not the only method, there are a lot of methods and ways to post data through Java programme. i don't have time to go through them all hence my simple unelegant methodology. Feel free to add more of your suggestion or better way. Thank you Naren Mane and Tim Moores for replying.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic