mmo*_*ris 43 webserver wolfram-mathematica
我的任务是使用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>
Run Code Online (Sandbox Code Playgroud)
我将收到的示例(XML):
<UserPluginInfo>
<PluginInfo>
<param name="pluginUid" type="string">1</param>
</PluginInfo>
<UserInfo>
<param name="username" type="string">USERNAME</param>
</UserInfo>
</UserPluginInfo>
Run Code Online (Sandbox Code Playgroud)
我发现Robert Raguet-Schofield在2009年撰写了一篇关于与Twitter交互的博客,该博客使用J/Link访问Java来执行HTTP POST并处理响应.
我的问题是,这是完成我的任务的最佳方法,还是自2009年以来Mathematica发展并且有更好的方法(更直接)完成我的任务?
虽然这可能不是更好的方法,但规避 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...
?>
Run Code Online (Sandbox Code Playgroud)
从 Mathmatica 的角度来看,这要简单得多,因为您只需使用内置import方法在代理页面上发出标准请求,但从登录页面上的请求GET获取结果 XML 。POST