简体   繁体   English

如何使用HttpURLConnection在Java中转换以下curl命令?

[英]How to convert the below curl command in Java using HttpURLConnection?

如何使用HttpURLConnection将以下curl命令转换为Java?

curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "https://example.com:8081/rest/api/2/issue/QA-31"
  1. How to set headers: 如何设置标题:

      HttpURLConnection myCon = (HttpURLConnection) new URL("https://example.com:8081/rest/api/2/issue/QA-31").openConnection(); myCon.setRequestMethod("GET"); myCon.setRequestProperty("Content-Type","application/json"); myCon.setRequestProperty("Authorization", "Basic ZnJlZDpmcmVk"); 
  2. After that you can read URL content in two ways 之后,您可以通过两种方式读取URL内容

    a. 一种。 you can get InputStream from Connection as 您可以从Connection获取InputStream为

     InputStream inStream = myCon.getInputStream(); 

and read it as you want directly or with some reader etc. 并根据需要直接阅读或与其他读者一起阅读。

b. by getting Content as 通过获取内容为

Object content = myCon.getContent();

For details read Java Docs for those methods it is very well documented. 有关详细信息,请阅读Java Docs中有关这些方法的详细记录。

BTW: keep in mind you have an SSL connection then you have to have remote server certificate in your java Key Store. 顺便说一句:请记住,您具有SSL连接,然后必须在Java密钥库中拥有远程服务器证书。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM