There are instances in which we need to test the access to a remote server via a http post request using url rather than a web service . Recently we were assigned a task by a client whose architectural team developed the access to their sub systems via a http post. Initially I didnt understand what they meant .
I tried various means to get the response from their subsystem using the web service client. I used the web service client invocation techniques in java/j2ee .It always gave an error which we couldnt comprehend. The architectural team of the client couldnt help much because they are experts in microsoft related technologies.
They just told we need to use something equivalent to the object in .net called Webrequest. I tried to install the free version of MS web development platform to use this Webrequest stuff. I was not successful in that . This task was in telecom domain in which we had no experience. I couldnt visualize what they meant by “HTTP XML POST to a remote server via URL” in the requirement. All my attempts to use the web service techniques for remote system invocation hit the wall
search using the all knowing google were in vain.
Finally after 3 days I found a solution by trial and error.
The client’s architectural team were indeed right . they designed the subsystem to work independent of the caller’s technology . I used the equivalent of webrequest in a client side code.
Infact it works correct whether the code is client side or serverside. A browser like IE/Firefox is sufficient to test. no serverside setup like apache/ tomcat is necessary
We were getting the expected response for a given xml input. I tested the code using IE and php. I have been getting the result (image) below . for security reasons I have hidden the input values in xml. But I am getting 200 OK response alongwith the xml response tag values.
corresponding code in html,javascript and php are given . Both are independent. Try to test using the browser first. you can use CURL module for php as in code. CURL should have been enabled in your php setup/host.
code in html,javascript
<html>
<head>
<script type=”text/javascript”>
function loadDoc(url)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.open(“POST”,url,true);
xmlhttp.setRequestHeader(‘Content-Type’, ‘text/xml’);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(‘A1′).innerHTML=xmlhttp.status;
document.getElementById(‘A2′).innerHTML=xmlhttp.statusText;
document.getElementById(‘A3′).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send(escape(document.forms[0].myText.value));
}
</script>
</head>
<body>
<h2>Retrieve data from a remote server using HTTP XML POST</h2>
<form><textarea name=”myText” id=”myText” cols=”100″ rows=”5″ ></textarea>
<p><b>Status:</b><span id=”A1″></span></p>
<p><b>Status text:</b><span id=”A2″></span></p>
<p><b>Response:</b><span id=”A3″></span></p>
<button onclick=”loadDoc(‘http://yourdomain.com/path/to/urlpostservice’)”>Get XML data</button>
</form>
</body>
</html>
code in php
<?php
$xml_data =’<get-customer-balance version=”1″>’.
‘<authentication>’.
‘<username></username>’.
‘<password></password>’.
‘</authentication>’.
‘<number></number>’.
‘</get-customer-balance>’;
$URL = “http://yourdomain.com/path/to/urlpostservice“;
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: text/xml’));
curl_setopt($ch, CURLOPT_POSTFIELDS, “$xml_data”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
?>
Ofcourse the xml,host name will vary for you. which needs to be given by your customer
.
The concept is same for all languages. It works in java as well.
If you have problem regarding the output for a given input xml , I will fix for free. Write to me .
Happy testing .
{ 2 comments }
