我的任务是使用 Mathematica 通过 JSP 使用 HTTP POST 和 XML 与第三方的 Web 服务器进行交互。我需要发送的示例:
<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>
我将收到的示例(XML):
<UserPluginInfo>
<PluginInfo>
<param name="pluginUid" type="string">1</param>
</PluginInfo>
<UserInfo>
<param name="username" type="string">USERNAME</param>
</UserInfo>
</UserPluginInfo>
我找到了一个 blog by Robert Raguet-Schofield 2009 年撰写的关于与 Twitter 交互的文章,使用 J/Link 访问 Java 以执行 HTTP POST 并处理响应。
我的问题是,这是完成我的任务的最佳方法还是 Mathematica 自 2009 年以来的发展,并且有更好的方法(更直接)来完成我的任务?
最佳答案
虽然这可能不是更好的方法,但另一种绕过 J/Link 需求的方法是设置一个中间 CGI 脚本来转换来自 GET
的请求。至 POST
为你。
该脚本文件将位于可访问的服务器上,它将采用指定的 GET 查询,在目标页面上发出 POST 请求,然后以正常方式输出/返回结果 XML。
例如,使用 curl
在 PHP 中这会很好地工作 - 尽管显然可以在几乎任何 CGI 语言中实现相同的功能。
<?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...
?>
从 Mathmatica 的角度来看,这要简单得多,因为您只需使用内置的
import
制定标准的方法 GET
在代理页面上请求但从 POST
获取结果 XML在登录页面上请求。
关于wolfram-mathematica - Mathematica 8.0 使用 HTTP POST 和 XML 与 Web 服务器 JSP 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8362461/