【问题标题】:HTTP Get request to Challonge returns robots.txt对 Chalonge 的 HTTP Get 请求返回 robots.txt
【发布时间】:2016-09-12 11:21:54
【问题描述】:

发送获取请求以尝试检索挑战括号时,我得到的是 robots.txt 文件,而不是实际的括号。如果我将相同的 url 复制并粘贴到浏览器中,我会得到预期的 JSON 括号。我想知道我做错了什么以及如何做到这一点,以便以下 Java 方法实际上返回括号而不是 metaname=robots 文本文件。

public String httpGett(String url,String userAgent) throws Exception{
    String USER_AGENT = userAgent;
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    con.setRequestMethod("GET");

    con.setRequestProperty("User-Agent", USER_AGENT);

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //System.out.println(response.toString());
    return(response.toString());
}

例如,如果我发送的网址是

https://challonge.com/api/tournaments/example.json?include_matches=1&include_participants=1&api_key=[MY API KEY]

然后我在浏览器中输入了网址,我会得到: http://pastebin.com/4W4kmdJV

如果我使用我的 Java 方法发送 get 请求,我会得到: http://pastebin.com/ifYSSzu3

如何从我的 Java 方法中获取正确的括号信息?

【问题讨论】:

    标签: java json rest


    【解决方案1】:

    所以我设法弄清楚我做错了什么。

    当我使用: https://api.challonge.com/v1/tournaments/example.json?include_matches=1&include_participants=1&api_key=[MYAPIKEY]

    metaname=robots 链接不再出现,而不是之前的链接,我得到了正确的括号信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-10-16
      相关资源
      最近更新 更多