英文:
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("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",
...
专注分享java语言的经验与见解,让所有开发者获益!
评论