1

I want to get Json from https://anapioficeandfire.com/api/characters/583 with native Java

Here is the code:

import java.net.*;
import java.io.*;

public class Main
{
    public static void main(String[] args) throws Exception {

        URL oracle = new URL("https://anapioficeandfire.com/api/characters/583");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
}

What I get is this error:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://anapioficeandfire.com/api/characters/583
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at java.net.URL.openStream(URL.java:1045)
    at sprint2.fireandice.Main.main(Main.java:17)

This code works with example.com, google.com etc...

Demaunt
  • 1,183
  • 2
  • 16
  • 26
  • The answer in https://stackoverflow.com/questions/13670692/403-forbidden-with-java-but-not-web-browser should help you out – Robbie Jones Aug 21 '17 at 07:11

1 Answers1

3

The problem is with openStream(). The server rejects this Connection and sends 403 Forbidden. You can "fool" the server and act like a normal browser by setting a user-agent.

public static void main(String[] args) throws Exception{

    URL oracle = new URL("https://anapioficeandfire.com/api/characters/583");
    HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();
    httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");

    BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
} 
Tobias Geiselmann
  • 2,139
  • 2
  • 23
  • 36
  • 1
    Thx a lot, it worked even without typing .openConnection() to (HttpURLConnection). URLConnection myUrlConnection = myUrl.openConnection(); myUrlConnection.addRequestProperty("User-Agent", "Mozilla/4.0"); – Demaunt Aug 21 '17 at 08:14
  • Awesome answer, I knew it was something to do with the server thinking I was doing something wrong. I just did not know what to look for. – LatheElHafy Feb 19 '18 at 04:50
  • Amazon cloudfront requires httpcon.addRequestProperty("Accept-Encoding", "gzip, deflate, br"); – XX Terror Sep 05 '20 at 15:53