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



我学到了很多在Java或其衍生物中运行curl的建议。例如,Java中的curl命令,Java中使用curl命令等。

此外,我还了解了如何使用DOI获取给定资源的元数据。根据这条指令,我对使用Java中的一个小片段来处理结果来运行这个curl命令非常感兴趣。

让我们举一个例子。URL为http://dx.doi.org/10.1016/j.immuni.2015.09.001

从终端运行curl命令

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

输出看起来像

@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}

在Groovy中运行这个curl命令

回收一些代码共享在这个网站上,我写了如下的过程。

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()

我得到的是HTML格式的整个页面,而不是我所期望的BibTeX格式的表单。

问题是:我做错了什么?你们中有人经历过这个问题吗

使用exec不是shell-您不能也不必为一个贝壳,它不在那里。默认情况下进一步使用exec(String)字符串标记器(基本上在空白处进行拆分(对于任何稍微高级的用例来说尤其无用。

你最好使用接受的版本命令的字符串数组(+args(。

你在哪里有效地打电话看起来是这样的(注意命令在空白处被拆分——所以我使用'来制作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 ...

使用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}
}

如果您需要";shell ism";,则改用CCD_ 8。

最新更新