[英]Curl POST request in java with variables
so I have this snip of code where I am trying to validate an xml with a certain schema using a previous web app that was developed. 所以我有这段代码,我试图使用以前开发的Web应用程序使用特定的架构来验证xml。 On the website it gave a curl command and I did some research on trying to execute it in java with no avail.
在网站上,它给出了curl命令,我做了一些研究,试图在Java中无济于事地执行它。 I found an api called httpclient but couldn't find anything for POST
我找到了一个名为httpclient的api,但找不到用于POST的任何内容
Second half of the question; 问题的后半部分; if this is the correct format then it must be how I presented the xml variable in the curl command.
如果这是正确的格式,那么它一定是我在curl命令中显示xml变量的方式。
@POST
@Path("/Validate")
//Uncomment when testing with real xml
//@Produces("application/xml")
public String validate(@FormParam("xml") String xml)
{
System.out.println(xml);
System.out.println("validate");
try {
Process process = Runtime.getRuntime().exec( "curl -X POST --data- urlencode 'xml=<hml>xml</hml>' http://miring.b12x.org/validator/ValidateMiring/ ");
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String cmdXML="";
String line=null;
while((line=input.readLine())!=null)
{
cmdXML+=line;
}
} catch (IOException e) {
logger.error("Something went wrong in validating "+ e);
System.out.println("Error in validation");
}
return "stuff";
}
First, runtime.exec(...)
requires a String[]
when your command has more than one element. 首先,当命令包含多个元素时,
runtime.exec(...)
需要String[]
。
Java API Runtime : http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[]) Java API运行时: http : //docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String [])
Second, you must ensure that command works on your side : 其次,您必须确保该命令对您有用:
curl -X POST --data-urlencode 'xml=<hml>xml</hml>' http://miring.b12x.org/validator/ValidateMiring/
Finally, you must indicate the full path of the command curl
. 最后,您必须指出命令
curl
的完整路径。
Example : /usr/bin/curl
on Unix or C:/Program Files/Curl/curl
on Windows 示例:在Unix上为
/usr/bin/curl
,在Windows上为C:/Program Files/Curl/curl
Be careful 小心
On the code below, you have to replace <PATH_TO_CURL>
by the actual path of the command curl
下面的代码,则必须更换
<PATH_TO_CURL>
由命令的实际路径curl
Can you try this code ? 你可以试试这个代码吗?
Code 码
@POST
@Path("/Validate")
//Uncomment when testing with real xml
//@Produces("application/xml")
public String validate(@FormParam("xml") String xml)
{
System.out.println(xml);
System.out.println("validate");
try {
Runtime runtime = Runtime.getRuntime();
String[] command = {"<PATH_TO_CURL>/curl", "-X", "POST", "--data-urlencode", "'xml=<hml>xml</hml>'", " http://miring.b12x.org/validator/ValidateMiring/"}
Process process = runtime.exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String cmdXML="";
String line=null;
while((line=input.readLine())!=null)
{
cmdXML+=line;
}
} catch (IOException e) {
logger.error("Something went wrong in validating "+ e);
System.out.println("Error in validation");
}
return "stuff";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.