【问题标题】:Server returned HTTP response code: 400 for URL:服务器返回 HTTP 响应代码:400 用于 URL:
【发布时间】:2016-08-03 14:30:35
【问题描述】:

我收到此错误:
java.io.IOException:服务器返回 HTTP 响应代码:400 用于 URL:https://graph.facebook.com/search?q=java&type=post 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(未知来源) 在 java.net.URL.openStream(未知来源) 在 Main.main(Main.java:19)

对于此代码:

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import org.json.JSONObject;
import javax.json.*;



public class Main {
    public static void main(String[] args) {
          try {
              URL url = new URL("https://graph.facebook.com/search?q=java&type=post");
              InputStream is = url.openStream();
              JsonReader rdr = Json.createReader(is);        
              JsonObject obj = rdr.readObject();
              JsonArray results = obj.getJsonArray("data");
              for (JsonObject result : results.getValuesAs(JsonObject.class)) {
                  System.out.print(result.getJsonObject("from").getString("name"));
                  System.out.print(": ");
                  System.out.println(result.getString("message", ""));
                  System.out.println("-----------");
              }
          } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我不明白为什么?

【问题讨论】:

  • HTTP 400 表示:由于语法错误,服务器无法理解请求。客户端不应该在没有修改的情况下重复请求。

标签: java json


【解决方案1】:

在浏览器中尝试该 URL 时,我得到响应:

{
   "error": {
      "message": "An access token is required to request this resource.",
      "type": "OAuthException",
      "code": 104,
      "fbtrace_id": "Du6VLoot53K"
   }
}

它还有一个 400 的响应代码,因此您似乎很清楚为什么会得到它:
需要访问令牌。

我在您的代码中没有看到任何试图提供此类访问令牌的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多