【问题标题】:Getting Exception in thread "main" java.lang.RuntimeException: Failed : HTTP error code : 401在线程“main”java.lang.RuntimeException 中获取异常:失败:HTTP 错误代码:401
【发布时间】:2016-11-29 15:19:26
【问题描述】:

我得到了

线程“主”java.lang.RuntimeException 中的异常:失败:HTTP 错误代码:jasonreader.main(jasonreader.java:21) 处的 401

在执行以下代码时,我正在尝试从其他 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 表示您无权访问该网页。首先,您必须自己授权。
  • 顺便说一句。 JSON 是一种数据格式。 Jason 是某人的名字。
  • @cricket_007,哈哈............:)

标签: java http


【解决方案1】:

向 Postman 提出这个请求,我得到了这个身体:

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

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

【讨论】:

    猜你喜欢
    • 2017-03-01
    • 2020-01-22
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 2014-05-17
    • 2023-04-04
    相关资源
    最近更新 更多