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...
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();
}
Related
I need to add a few cookies to authorize on website. Cookies are added successfully, but they are missing when making a request:
import java.io.*;
import java.net.*;
public class Main {
static public void main(String[] args) throws Exception {
CookieManager cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
CookieStore cookieJar = cookieManager.getCookieStore();
CookieHandler.setDefault(cookieManager);
HttpCookie cookie = new HttpCookie("name123", "value123");
cookieJar.add(new URI("http://httpbin.org"), cookie);
HttpURLConnection connection = (HttpURLConnection) new URL("http://httpbin.org/cookies").openConnection();
connection.setRequestMethod("GET");
connection.connect();
BufferedReader in;
StringBuilder response = new StringBuilder();
String inputLine;
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
But I only get an empty map of cookies in response
{ "cookies": {}}
Please, tell me what I should do to fix it.
cookie.setPath( "/" );
cookie.setVersion( 0 );
Do the trick ¯\_(ツ)_/¯
The simple Java code below works. Is there an easy way to find out / inspect the HTTP request (not the response) headers actually sent?
import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;
public class Test {
public static void main(String[] args) throws Exception {
String httpsURL = "https://api.gdax.com/products/BTC-USD/book?level=1";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
InputStream ins = con.getInputStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
}
}
I think you can not do it programaticaly, you could use some kind of proxy like TCP/IP monitor in Eclipse. Or enable the debbug option for java adding these options:
-Djava.util.logging.config.file=logging.properties
And put in logging.properties (by default in JRE_HOME\lib) the following property
sun.net.www.protocol.http.HttpsURLConnection.level = ALL
I want to get the values of a weather api from
http://www.dataweave.in/apis/usage/13/Indian-Weather-Data
Like in PHP, the syntax to get the values from the api is:
$String=file_get_contents("url of the api");
But i would like to do the same in java. How can i get the file contents from the api in Java?
You can use something like below:
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://api.dataweave.in/v1/indian_weather/findByCity/?api_key=b20a79e582ee4953ceccf41ac28aa08d&city=Agartala&date=20120501");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
inputLine has your data.
Just I am trying to read the HTML file using URL. But getting the timed out exception.
source code:
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
System.setProperty("java.net.useSystemProxies","true");
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
This code works fine for me, please check your internet connection..
is it possible to open urls in BlueJ.
For example in PHP I can use the function file_get_contents()
Is there something equal in BlueJ?
You need to look at using a buffered reader and using the URL object in Java.
It's basically something like this:
URL url = new URL("http://myurl.com");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String input = "";
while((input = reader.readLine())!=null) {
System.out.println(input);
}
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}