Reputation: 45
In some code i use similar code to Oracle's tutorial: Reading directly from a URL. Oracle's code also here below:
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();
}
}
This code worked before but it doesnt anymore. Even if i try the exact same code, inputLine never has any value. I dont know if it has something to do with java versions or something elso but i would like to know why this happens, and what is a good alternative.
Upvotes: 1
Views: 164
Reputation: 45
As loris securo commented i needed to use https instead of http. And that fixed my problems.
Upvotes: 0
Reputation: 62743
If you replace http://www.oracle.com/ by http://www.google.com/ it works.
It's because a GET http://www.oracle.com/ gives
HTTP/1.1 301 Moved Permanently
If you run
curl www.oracle.com
You obtain the same effect. You have to follow the redirect with
curl -L www.oracle.com
to obtain the html content. In Java, you also have to follow the redirect, such as in this article :
https://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/
Upvotes: 1
Reputation: 56489
Oracle -
When you run the program, you should see, scrolling by in your command window, the HTML commands and textual content from the HTML file located at http://www.oracle.com/. Alternatively, the program might hang or you might see an exception stack trace. If either of the latter two events occurs, you may have to set the proxy host so that the program can find the Oracle server.
UNIX
java -Dhttp.proxyHost=proxyhost [-Dhttp.proxyPort=portNumber] URLReader
DOS shell (Windows 95/NT)
java -Dhttp.proxyHost=proxyhost [-Dhttp.proxyPort=portNumber] URLReader
Also, I have actually ran it on my IDE but it only works with HTTPS
Upvotes: 0