0
我越來越入門例外在線程 「主」 了java.lang.RuntimeException:失敗:HTTP錯誤代碼:401
異常在線程 「主」 了java.lang.RuntimeException:失敗:HTTP錯誤代碼: 401在jasonreader.main(jasonreader.java:21)
在執行下面的代碼時,我試圖從其餘的API獲取數據。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class jasonreader {
// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String[] args) {
try {
URL url = new URL(" https://api.linkedin.com/v1/companies/1337/updates?start=20&count=10&format=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
401表示您無權訪問該網頁。起初,您必須授權您自己。順便說一句, – TMichelsen
。 JSON是一種數據格式。傑森是某人的名字。 –
@ cricket_007,哈哈............ :) –