Re: WEB Import using "POST" instead of "GET"
- To: mathgroup at smc.vnet.net
- Subject: [mg103264] Re: [mg103255] WEB Import using "POST" instead of "GET"
- From: "Scot T. Martin" <smartin at seas.harvard.edu>
- Date: Fri, 11 Sep 2009 19:57:42 -0400 (EDT)
- References: <200909111136.HAA05541@smc.vnet.net>
To my knowledge, you cannot (unfortunately) do this directly with Import or any other way with the direct built-in commands in Mathematica (maybe to come in version 8)? However, Mathematica has built-in Java, and you should be able to do it that way. I have not specifically tested POST in that exact configuration, but I have tested POST using a Java set up with webMathematica, and the Java-Mathematica interface works great. I therefore think it should work great with the built-in Mathematica Java---my only hesitation here is that I have not tested, and it's always possible that Mathematica's built-in Java capability does not have the exact same specifications or capabilities to incorporate java classes as the JSP server that runs webMathematica. I'll post below the webMathematica code that I use. The <msp> tags are the way that Mathematica communicates with JSP. The important part of this code for trying to do directly within Java of Mathematica is import="java.net.*,java.io.*" If anyone in the forum can confirm that these are available in Mathematica's Java implementation, then you can definitely use this route to get to Mathematica-POST interactions. As I said, I am successfully doing this in the code below that interfaces Mathematica and Java using the JSP server of webMathematica. CODE: <%@ page language="java" import="java.net.*,java.io.*" %> <%@ taglib uri="/webMathematica-taglib" prefix="msp" %> <html> <head> <title>Get some data from AIM website</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#FFFFFF" text="#000000"> <h1>This page was loaded with a POST command.</h1> <% String data = URLEncoder.encode("wwwUsageType", "UTF-8") + "=" + URLEncoder.encode("calculation", "UTF-8"); data += "&" + URLEncoder.encode("wwwOutputMode", "UTF-8") + "=" + URLEncoder.encode("normal", "UTF-8"); data += "&" + URLEncoder.encode("wwwInputMode", "UTF-8") + "=" + URLEncoder.encode("interactive", "UTF-8"); data += "&" + URLEncoder.encode("interactive_type", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8"); data += "&" + URLEncoder.encode("Model", "UTF-8") + "=" + URLEncoder.encode("ModelII", "UTF-8"); data += "&" + URLEncoder.encode("iCaseInorg", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8"); data += "&" + URLEncoder.encode("ExcludeWater", "UTF-8") + "=" + URLEncoder.encode("y", "UTF-8"); data += "&" + URLEncoder.encode("OrgFileName", "UTF-8") + "=" + URLEncoder.encode("nullname", "UTF-8"); data += "&" + URLEncoder.encode("hydroxide", "UTF-8") + "=" + URLEncoder.encode("0.D0", "UTF-8"); data += "&" + URLEncoder.encode("ammonia", "UTF-8") + "=" + URLEncoder.encode("0.D0", "UTF-8"); data += "&" + URLEncoder.encode("water_dissoc", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8"); data += "&" + URLEncoder.encode("pressure", "UTF-8") + "=" + URLEncoder.encode("1.D0", "UTF-8"); data += "&" + URLEncoder.encode("volume", "UTF-8") + "=" + URLEncoder.encode("1.D0", "UTF-8"); data += "&" + URLEncoder.encode("h2og", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8"); data += "&" + URLEncoder.encode("hno3g", "UTF-8") + "=" + URLEncoder.encode("4", "UTF-8"); data += "&" + URLEncoder.encode("hclg", "UTF-8") + "=" + URLEncoder.encode("4", "UTF-8"); data += "&" + URLEncoder.encode("h2so4g", "UTF-8") + "=" + URLEncoder.encode("4", "UTF-8"); data += "&" + URLEncoder.encode("nh3g", "UTF-8") + "=" + URLEncoder.encode("4", "UTF-8"); data += "&" + URLEncoder.encode("temperature", "UTF-8") + "=" + URLEncoder.encode("298.15", "UTF-8"); data += "&" + URLEncoder.encode("water_var", "UTF-8") + "=" + URLEncoder.encode(".80", "UTF-8"); data += "&" + URLEncoder.encode("hydrogen", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8"); data += "&" + URLEncoder.encode("ammonium", "UTF-8") + "=" + URLEncoder.encode("2", "UTF-8"); data += "&" + URLEncoder.encode("sulphate", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8"); data += "&" + URLEncoder.encode("nitrate", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8"); data += "&" + URLEncoder.encode("nCompounds", "UTF-8") + "=" + URLEncoder.encode("0", "UTF-8"); data += "&" + URLEncoder.encode("nh42so4", "UTF-8") + "=" + URLEncoder.encode("4", "UTF-8"); URL url = new URL("http://URL REDACTED HERE FOR PRIVACY.com/cgi-bin/eaim25"); // POST to website URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // READ from website String result = null; StringBuffer sb = new StringBuffer(); String line; BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = rd.readLine()) != null) { sb.append((line+"\r")); // ADD <CR> to each line } wr.close(); rd.close(); result = sb.toString(); %> <hr> <h1>The page as produced by Java Server Page</h1> <%= result %> <hr> <h1>The page as produced when imported into Mathematica</h1> <msp:allocateKernel> <msp:set name="postresult" objectValue="<%= result %>" /> <msp:evaluate> postresult </msp:evaluate> <hr> <h1>The page as produced after some manipulation in Mathematica</h1> <msp:evaluate> test=StringSplit[postresult,"\r"] </msp:evaluate> <msp:evaluate> "There are "<>ToString[Length[test]]<>" lines in the output (counted by Mathematica)" </msp:evaluate> </msp:allocateKernel> </body> </html> On Fri, 11 Sep 2009, Nacho wrote: > Hello Mathematica users. > > I would like to access to some Web data using Import[], but the web I > need uses HTTP POST to give some variables, and not GET. > > Is there any way to force Mathematica to use POST, with some provided > variables? > > Thank you. > > >
- References:
- WEB Import using "POST" instead of "GET"
- From: Nacho <ncc1701zzz@gmail.com>
- WEB Import using "POST" instead of "GET"