【问题标题】:Mathematica 8.0 interaction with a web server JSP using HTTP POST and XMLMathematica 8.0 使用 HTTP POST 和 XML 与 Web 服务器 JSP 交互
【发布时间】:2012-01-11 20:15:59
【问题描述】:

我的任务是使用 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>

我发现了一篇写于 2009 年的blog by Robert Raguet-Schofield,关于与 Twitter 交互,它使用 J/Link 访问 Java 以执行 HTTP POST 并处理响应。

我的问题是,这是完成我的任务的最佳方法还是 Mathematica 自 2009 年以来已经发展,并且有更好的方法(更直接)来完成我的任务?

【问题讨论】:

标签: wolfram-mathematica webserver


【解决方案1】:

虽然这可能不是更好的方法,但绕过 J/Link 需要的另一种方法是设置一个中间 CGI 脚本,为您将请求从 GET 转换为 POST

此脚本文件将位于可访问的服务器上,它将接受指定的 GET 查询,在目标页面上发出 POST 请求,然后以正常方式输出/返回结果 XML。

例如,在 PHP 中使用 curl 会很好地工作 - 尽管显然可以在几乎任何 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 .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2010-12-02
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多