简体   繁体   中英

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

    a. you can get InputStream from Connection as

     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.

BTW: keep in mind you have an SSL connection then you have to have remote server certificate in your java Key Store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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