在线程" main"中获得异常java.lang.RuntimeException:失败:HTTP错误代码:401

时间:2016-07-26 05:58:21

标签: java http

我正在

  

线程中的异常" main" java.lang.RuntimeException:失败:HTTP错误代码:jasonreader.main上的401(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();

      }

    }

}

1 个答案:

答案 0 :(得分:0)

与邮递员一起做这个请求,我得到了这个机构:

{
"errorCode": 0,
"message": "Unknown authentication scheme",
"requestId": "JP2QSQXZTG",
"status": 401,
"timestamp": 1469513993009
}

这意味着您需要访问令牌。请参阅this link for more infoLinkedin documentation