简体   繁体   English

为什么在终端和 Java 中运行 curl 命令时得到不同的结果?

[英]Why am I obtaining different results when running curl command from Terminal and in Java?

I have learned lots of suggestions to run curl in Java or its derivatives.我学到了很多关于在 Java 或其衍生产品中运行curl的建议。 For example, curl command in Java , using curl command in Java , etc.例如, Java 中的 curl 命令,Java使用 curl 命令等。

Also, I have figured out how to fetch the metadata of a given resource using DOI .此外,我已经弄清楚如何使用 DOI 获取给定资源的元数据 From this instruction, I am very interested in running this curl command using a small snippet in Java to handle the result.从这个指令中,我对使用 Java 中的一个小片段运行这个 curl 命令来处理结果非常感兴趣。

Let's give an example.让我们举一个例子。 The URL is http://dx.doi.org/10.1016/j.immuni.2015.09.001 . URL 是http://dx.doi.org/10.1016/j.immuni.2015.09.001

Running curl command from a terminal从终端运行 curl 命令

curl -LH "Accept: application/x-bibtex" http://dx.doi.org/10.1016/j.immuni.2015.09.001

The output looks like输出看起来像

@article{Biswas_2015,
    doi = {10.1016/j.immuni.2015.09.001},
    url = {https://doi.org/10.1016%2Fj.immuni.2015.09.001},
    year = 2015,
    month = {sep},
    publisher = {Elsevier {BV}},
    volume = {43},
    number = {3},
    pages = {435--449},
    author = {Subhra~K. Biswas},
    title = {Metabolic Reprogramming of Immune Cells in Cancer Progression},
    journal = {Immunity}

Running this curl command in Groovy在 Groovy 中运行此 curl 命令

Recycling some codes sharing on this site, I have written the process as below.回收本站分享的一些代码,我写的过程如下。

Map result = [:]
String command = "curl -LH 'Accept: application/x-bibtex' http://dx.doi.org/10.1016/j.immuni.2015.09.001"
Process process = Runtime.getRuntime().exec(command)
InputStream stream = process.getInputStream()
result.put("data", stream.text)
process.destroy()

What I obtain is the whole page in HTML rather than a BibTeX formatted form as what is my expectation.我得到的是 HTML 中的整个页面,而不是我所期望的 BibTeX 格式的表单。

The question is: what am I doing wrong here?问题是:我在这里做错了什么? Are there any of you that have experienced with that issue?你们中有人遇到过这个问题吗?

Using exec is not a shell - you can't and don't have to quote for a shell, that is not there.使用exec不是 shell - 你不能也不必为 shell 引用,那不存在。 Further exec(String) uses by default a string tokenizer (which basically splits at whitespace) to make it particularly useless for any slightly advanced usecase.进一步的exec(String)默认使用字符串标记器(它基本上在空格处拆分)使其对于任何稍微高级的用例特别无用。

You are most likely always better off to use the version that accepts a string array for the command (+ args).您很可能总是最好使用接受命令字符串数组的版本(+ args)。

What you where effectively calling looked like this (note, that the command gets split at whitespace -- so I used \\' to make my shell ignore that):您有效调用的内容如下所示(请注意,该命令在空格处被拆分 - 所以我使用\\'使我的 shell 忽略它):

# curl -LH \'Accept: application/x-bibtex\' http://dx.doi.org/10.1016/j.immuni.2015.09.001
curl: (6) Could not resolve host: application
... HTML ...

The shortest route using groovy looks like this (note that exec also has a version for passing in an array of strings):使用 groovy 的最短路径如下所示(注意exec也有一个用于传入字符串数组的版本):

groovy:000> ["curl", "-LH", "Accept: application/x-bibtex", "http://dx.doi.org/10.1016/j.immuni.2015.09.001"].execute().text
===> @article{Biswas_2015,
9doi = {10.1016/j.immuni.2015.09.001},
9url = {https://doi.org/10.1016%2Fj.immuni.2015.09.001},
9year = 2015,
9month = {sep},
9publisher = {Elsevier {BV}},
9volume = {43},
9number = {3},
9pages = {435--449},
9author = {Subhra~K. Biswas},
9title = {Metabolic Reprogramming of Immune Cells in Cancer Progression},
9journal = {Immunity}
}

If you need "shell-isms", then use ["sh", "-c", command] instead.如果您需要“shell-isms”,请改用["sh", "-c", command]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在java中运行外部程序会产生与terminal命令不同的结果 - Running external program within java produces different results from terminal command 当我从终端手动运行Java程序时,它运行正常,但是当我从Eclipse运行时,它却无法运行 - When I am running java program manually from terminal it is working fine but it is not working when i am running from eclipse 当我从终端运行Java程序时缺少属性文件 - Missing properties file when i am running a java program from terminal 从Java运行时与在终端中运行时,SVN Info命令有所不同 - SVN Info command different when run from Java than in terminal Java文件构建-为什么我得到不同的结果? - Java file construction - why am I getting different results? 从命令提示符运行JAR文件时,为什么会出现异常? - Why am I getting an exception when running a JAR file from the command prompt? 从Java在MacOs终端中运行命令 - Running a command in MacOs terminal from java 当我从 Linux 的命令行运行 Java class 文件时,为什么会出现 NoClassDefFoundError? - Why am I getting a NoClassDefFoundError when I run a Java class file from the command line in Linux? 为什么该Java程序在Eclipse上给出错误的结果,而从终端运行时却给出正确的结果? - Why is this Java program gives incorrect results on Eclipse and correct results when run from terminal? 在命令行中从Java代码运行grep命令会得到不同的结果 - running grep command in command line and from java code gives different results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM