http - Java reading URL from anapioficeandfire returns 403 -


i want json https://anapioficeandfire.com/api/characters/583 native java

here 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 error:

exception in thread "main" java.io.ioexception: server returned http response code: 403 url: https://anapioficeandfire.com/api/characters/583     @ sun.net.www.protocol.http.httpurlconnection.getinputstream0(httpurlconnection.java:1876)     @ sun.net.www.protocol.http.httpurlconnection.getinputstream(httpurlconnection.java:1474)     @ sun.net.www.protocol.https.httpsurlconnectionimpl.getinputstream(httpsurlconnectionimpl.java:254)     @ java.net.url.openstream(url.java:1045)     @ sprint2.fireandice.main.main(main.java:17) 

this code works example.com, google.com etc...

the problem openstream(). server rejects connection , sends 403 forbidden. can "fool" server , act normal browser setting 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(); }  

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -