【发布时间】:2021-10-08 13:27:54
【问题描述】:
我需要把下面的curl命令转换成java命令。
curl https://api.linkedin.com/v2/me -H "Authorization: Bearer xxx"
我写了这段代码:
public static void main(String[] args) throws MalformedURLException, IOException{
HttpURLConnection con = (HttpURLConnection) new URL("https://api.linkedin.com/v2/me").openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Bearer xxx");
con.setDoOutput(true);
con.setDoInput(true);
con.getOutputStream().write("LOGIN".getBytes("UTF-8"));
con.getInputStream();
}
但我得到一个错误:
Exception in thread "main" java.io.FileNotFoundException: https://api.linkedin.com/v2/me
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1915)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
at testiamo.main(testiamo.java:18)
【问题讨论】:
-
试试
HttpsURLConnection con = (HttpsURLConnection)new URL("https://api.linkedin.com/v2/me").openConnection();(import javax.net.ssl.HttpsURLConnection;)