[英]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.