I have been tasked with using Mathematica to interact with a third party's web server via JSP using HTTP POST and XML. Example of what I need to send:
<html> <head></head> <body> <form method="post" action="http://www. ... .com/login.jsp"> <textarea name="xml" wrap="off" cols="80" rows="30" spellcheck="false"> <loginInfo> <param name="username" type="string">USERNAME</param> <param name="pwd" type="string">PASSWORD</param> </loginInfo> </textarea> <input type="hidden" name="Login" value="1"/> <input type="submit" name="go" value="go" /> </form> </body> </html>
Example of what I will receive (XML):
<UserPluginInfo> <PluginInfo> <param name="pluginUid" type="string">1</param> </PluginInfo> <UserInfo> <param name="username" type="string">USERNAME</param> </UserInfo> </UserPluginInfo>
I found a blog by Robert Raguet-Schofield written in 2009 about interacting with Twitter that uses J/Link to access Java to perform the HTTP POST and handle the response.
My question is, is this the best method to accomplish my task or has Mathematica evolved since 2009 and there is a better way (more straight forward) to accomplish my task?
Whilst this may not be a better way, an alternative approach to circumvent the need of J/Link would be to set up an intermediate CGI script to translate the request from GET
to POST
for you.
This script file would sit on an accessible server, it would take the specified GET query, make a POST request on the target page, then output/return the result XML in the normal way.
For example, using curl
in PHP that would work well - although obviously it would be possible to achieve the same functionality in pretty much any CGI language.
<?php $c = curl_init(); // set the various options, Url, POST,etc curl_setopt($c, CURLOPT_URL, "http://www. ... .com/login.jsp"); // Target page curl_setopt($c, CURLOPT_HEADER, false); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_RETURNTRANSFER, false); // POST the incomming query to the Target Page curl_setopt($c, CURLOPT_POSTFIELDS, $_SERVER['QUERY_STRING']); curl_exec($c); curl_close($c); // Output the XML response using header/echo/etc... // You might need to also send some of the POST request response headers // use CURLOPT_HEADER to access these... ?>
From the Mathmatica stand point this is much simpler as you simply use the built in import
method to make a standard GET
request on the proxy page but get the result XML from a POST
request on the login page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With