Java URLConnection - java

Simple stuff, I am learning URLs/Networking in my class and I am trying to display something on a webpage. Later I am going to connect it to a MySQL DB... anyway here is my program:
import java.net.*; import java.io.*;
public class asp {
public static URLConnection
connection;
public static void main(String[] args) {
try {
System.out.println("Hello World!"); // Display the string.
try {
URLConnection connection = new URL("post.php?players").openConnection();
}catch(MalformedURLException rex) {}
InputStream response =
connection.getInputStream();
System.out.println(response);
}catch(IOException ex) {}
} }
It compiles fine... but when I run it I get:
Hello World!
Exception in thread "main" java.lang.NullPointerException
at asp.main(asp.java:17)
Line 17: InputStream response = connection.getInputStream();
Thanks,
Dan

You have a malformed URL, but you wouldn't know because you swallowed its exception!
URL("post.php?players")
This URL is not complete, it misses the host (maybe localhost for you?), and the protocol part, say http so to avoid the malformed URL exception you have to provide the full URL including the protocol
new URL("http://www.somewhere-dan.com/post.php?players")
Use the Sun tutorials on URLConnection first. That snippet is at least known to work, if you substitute the URL in that example with a valid URL you should have a working piece of code.

It's because your URL is not valid. You need to put the full address to the page you are trying to open a connection to. You are catching the malformedurlexception but that means that there is no "connection" object at that point. You have an extra closed bracket after the first catch block it appears as well. You should put the line that you are getting the null pointer for and the system.out.println above the catch blocks
import java.net.*; import java.io.*;
public class asp {
public static URLConnection connection;
public static void main(String[] args) {
try {
System.out.println("Hello World!"); // Display the string.
try {
URLConnection connection = new URL("http://localhost/post.php?players").openConnection();
InputStream response = connection.getInputStream();
System.out.println(response);
}catch(MalformedURLException rex) {
System.out.println("Oops my url isn't right");
}catch(IOException ex) {}
}
}

Related

Why is this site returning a IOException with the Java openStream() method?

I am writing a program to output a website's HTML code. I have tested it on some sites such as https://www.stackoverflow.com and it works. However, when I tried running the program with https://www.science.energy.gov, it doesn't work and throws an IOException. If I change the https to http and run it with http://www.science.energy.gov, the program runs but does not print anything. I am not sure why the HTML code for the http website is not displaying.
Below is the relevant code for the HTML extraction program:
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL url;
InputStream is = null;
DataInputStream dis;
String line;
try {
url = new URL("https://science.energy.gov/");
is = url.openStream(); // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));
while ((line = dis.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
}
That's because when you send a request in http for http://science.energy.gov/ it redirects automatically to https, which means the site will reload. And your program is not capable of handling redirect requests. So it just stops. No output no error.
Now about the SSLHandshakeException. The error explains it self, unable to find valid certification path to requested target. Which means your java keystore doesn't have ssl certificate for service you are trying to connect. So you need to obtain the public certificate from the server you're trying to connect to. Read this answer for more information.
Also read,
How to solve javax.net.ssl.SSLHandshakeException Error?
javax.net.ssl.SSLHandshakeException

IOException Java

I tried to run this piece of code without an internet connection, expecting and IOException to trigger:
import java.net.*;
import java.io.*;
public class API_connect {
public static void main(String[] args) {
try {
URL API = new URL("http://api.football-data.org");
URLConnection API_connection = API.openConnection();
}
catch(MalformedURLException exception) {
System.out.print(exception);
}
catch(IOException exception) {
System.out.print(exception);
System.out.print("is something going on here?");
}
}
}
And well... To my surprise nothing was printed, and I can't figure out why. Wouldn't a lack of internet connection be the main reason why an IOException is thrown here?
openConnection() does not actually try to connect:
It should be noted that a URLConnection instance does not establish the actual network connection on creation. This will happen only when calling URLConnection.connect().
Try calling connect() on it.
Alternatively, you could try the following:
new URL(...).openStream().read();
That would actually try to read 1 byte from that url and would fail.

Java: Download from an URL

Could someone try my codes out? It was working a few days ago and now it's not. I did not modify anything, and so I suspect the webmaster of that side has block me. Could someone check it out for me? This is part of my school project.
public class Cost extends TimerTask{
public void run() {
Calendar rightNow = Calendar.getInstance();
Integer hour = rightNow.get(Calendar.HOUR_OF_DAY);
if (hour==1) {
try {
URL tariff = new URL("http://www.emcsg.com/MarketData/PriceInformation?downloadRealtime=true");
ReadableByteChannel tar = Channels.newChannel(tariff.openStream());
FileOutputStream fos = new FileOutputStream("test.csv");
fos.getChannel().transferFrom(tar, 0, 1<<24);
} catch (IOException ex) {
Logger.getLogger(Cost.class.getName()).log(Level.SEVERE, null, ex);
}
}
else {
}
}
}
First of all, clean up your IO exceptions as that might be obscuring the problem - check you can write to D:.
If you are being blocked by the site because of your user-agent header:
This will show you your user-agent header: http://pgl.yoyo.org/http/browser-headers.php. Then the answer to Setting user agent of a java URLConnection tells you how to set your header.
You will either need to add a step between instantiating URL and opening stream:
URL tariff = new URL("http://www.emcsg.com/MarketData/PriceInformation?downloadRealtime=true");
java.net.URLConnection c = tariff.openConnection();
c.setRequestProperty("User-Agent", " USER AGENT STRING HERE ");
ReadableByteChannel tar = Channels.newChannel(c.getInputStream());
or you could try just doing this:
System.setProperty("http.agent", " USER AGENT STRING HERE ");
sometime before you call openStream().
Edit: This works for me. Can you try running it and let us know the output:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class TestURL {
public static void main(String[] args) {
try {
URL tariff = new URL("http://www.emcsg.com/MarketData/PriceInformation?downloadRealtime=true");
URLConnection c = tariff.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
System.out.println(br.readLine());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I checked your code and running it I had no problem, everything works fine.
Are you working behind a proxy?
In that case you have to configure it:
System.setProperty("http.proxyHost", "my.proxy.name");
System.setProperty("http.proxyPort", "8080");

JavaSE 7 URL Timing out that works in JavaSE 6?

The following code works fine in JavaSE 6 but is throwing a ConnectException (timeout) when executed in JavaSE 7. Is this a JDK7 bug or bad code? I really don't understand...
public static void main(String[] args) {
try {
URL url = new URL("http://dl.dropbox.com/u/34206572/version.txt");
url.openConnection().connect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I tried this code in 1.7.0_02-b13, it works fine. I visit the link above, it is not available (page 404 is returned).
Maybe, you mean that the following code crashes:
public static void main(String[] args) throws Exception {
URL url = new URL("http://dl.dropbox.com/u/34206572/version.txt");
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
}
with following exception (I formatted it out):
Exception in thread "main" java.io.FileNotFoundException:
http://dl.dropbox.com/u/34206572/version.txt
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(
HttpURLConnection.java:1610)

Code to login not working

could anyone help me with figuring out why this code isn't working? It's supposed to log into HTS, but it doesn't work. It's not giving me any error messages or anything, just no result at all. Any help would be appreciated.
Here's the code:
import java.net.*;
import java.io.*;
public class Login {
private static URL URLObj;
private static URLConnection connect;
public static void main(String[] args) {
try {
URLObj = new URL("http://www.hackthissite.org/user/login");
connect = URLObj.openConnection();
connect.addRequestProperty("REFERER", "http://www.hackthissite.org");
connect.setDoOutput(true);
}
catch (MalformedURLException ex) {
System.out.println("The URL specified was unable to be parsed or uses an invalid protocol. Please try again.");
System.exit(1);
}
catch (Exception ex) {
System.out.println("An exception occurred. " + ex.getMessage());
System.exit(1);
}
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connect.getOutputStream()));
writer.write("username=BrandonHeat&password=**********&btn_submit=Login");
writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
String lineRead = "";
while ((lineRead = reader.readLine()) != null) {
System.out.println(lineRead);
}
reader.close();
}
catch (Exception ex) {
System.out.println("There was an error reading or writing to the URL: " + ex.getMessage());
}
}
}
i'm just guessing, but the documentation for URLConnection indicates that you must connect after having set the parameters :
1.The connection object is created by invoking the openConnection method on
a URL.
2.The setup parameters and general request properties are manipulated.
3.The actual connection to the remote object is made, using the connect
method.
4.The remote object becomes available. The header fields and the contents of
the remote object can be accessed.
...
The following methods are used to
access the header fields and the
contents after the connection is made
to the remote object:
getContent
getHeaderField
getInputStream
getOutputStream
Edit :
can't you open the URL with you parameters in the URL (ie http://www.hackthissite.org/user/login?username=BrandonHeat&password=xxxxx&btn_submit=Login), or setted as header ?
also, after the connect(), what do you get if you do a getContent() ?
Edit : there is a description of the process here.
Regards
Guillaume

Categories