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..
Related
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();
}
This question already has answers here:
Read url to string in few lines of java code
(11 answers)
Closed 5 years ago.
Consider the following.
val br = new BufferedReader(new FileReader(inputFileName))
But instead of a file, I want to read a file directly from the web, that is, from ftp or http. So, what would be the equivalent for reading from a URL?
It's URLConnection.
Here's an example from Java documentation : Reading from and Writing to a URLConnection :
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
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();
}
}
you can use java.io.InputStream
import java.net.;
import java.io.;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
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();
}
}
for more info see Reading Directly from a URL
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.
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();
}
}