将curl命令翻译成Java:

huangapple 未分类评论55阅读模式
英文:

translating curl command into java

问题

我必须翻译一个 curl 命令:
curl -X GET --header 'Accept: application/json' 'https://terminologies.gfbio.org/api/terminologies/'
转换为 Java 代码。
我想要将这个 HTTP 请求发送到指定的地址。然后将响应存储到 Java 的一个字符串中,请告诉我翻译结果。

英文:

I have to translate a curl command :
curl -X GET --header 'Accept: application/json' 'https://terminologies.gfbio.org/api/terminologies/'
to java.
I want to send this http request to the specified address. The response is then to be stored into a string in java, can you please tell me the translation?

答案1

得分: 0

你可以使用纯JAVA,无需额外的库来实现这一点。对于JAVA版本v11+,我会推荐@MarcoLucidi提出的方法。

String content = "";
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");

URL url = new URL("https://terminologies.gfbio.org/api/terminologies/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("accept", "application/json");

BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));

String inputLine;
while ((inputLine = in.readLine()) != null)
    content += inputLine;
in.close();

System.out.println(content);
英文:

You can archieve it without an additional library with plain JAVA. For JAVA versions v11+ I would prefer the approach @MarcoLucidi suggested.

 String content="";
 System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");

 URL url = new URL("https://terminologies.gfbio.org/api/terminologies/");
 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
 urlConnection.setRequestProperty("accept", "application/json");
 
 BufferedReader in = new BufferedReader(
 new InputStreamReader(urlConnection.getInputStream()));

 String inputLine;
 while ((inputLine = in.readLine()) != null)
     content+=inputLine;
 in.close();
 
 System.out.println(content);

答案2

得分: 0

如果您至少使用Java 11可以使用[新的内置HTTP客户端](https://openjdk.java.net/groups/net/httpclient/intro.html)。例如:

import java.io.IOException;
import java.net.URI;
import java.net.http.*;
import java.net.http.HttpResponse.BodyHandlers;

public class HttpExample
{
    public static void main(String[] args)
    {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest req = HttpRequest
                .newBuilder()
                .uri(URI.create("https://terminologies.gfbio.org/api/terminologies/"))
                .header("Accept", "application/json")
                .build();
        try {
            HttpResponse<String> resp = client.send(req, BodyHandlers.ofString());
            String body = resp.body();
            System.out.println(body);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
$ javac HttpExample.java
$ java HttpExample

{
    "request":{
        "query":"http://terminologies.gfbio.org/api/terminologies/",
        "executionTime":"Sun Jun 28 05:00:03 CEST 2020"
    },
    "results":[
        {
            "name":"Biological Collections Ontology",
            "acronym":"BCO",
    ...
}
英文:

if you are using at least java 11, you can use the new built in http client. for example:

import java.io.IOException;
import java.net.URI;
import java.net.http.*;
import java.net.http.HttpResponse.BodyHandlers;

public class HttpExample
{
        public static void main(String[] args)
        {
                HttpClient client = HttpClient.newHttpClient();
                HttpRequest req = HttpRequest
                        .newBuilder()
                        .uri(URI.create(&quot;https://terminologies.gfbio.org/api/terminologies/&quot;))
                        .header(&quot;Accept&quot;, &quot;application/json&quot;)
                        .build();
                try {
                        HttpResponse&lt;String&gt; resp = client.send(req, BodyHandlers.ofString());
                        String body = resp.body();
                        System.out.println(body);

                } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                }
        }
}

$ javac HttpExample.java
$ java HttpExample

{
    &quot;request&quot;:{
        &quot;query&quot;:&quot;http://terminologies.gfbio.org/api/terminologies/&quot;,
        &quot;executionTime&quot;:&quot;Sun Jun 28 05:00:03 CEST 2020&quot;
    },
    &quot;results&quot;:[
        {
            &quot;name&quot;:&quot;Biological Collections Ontology&quot;,
            &quot;acronym&quot;:&quot;BCO&quot;,
...

huangapple
  • 本文由 发表于 2020年6月29日 01:23:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/62625927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定