displaying URL contents via an applet

Y

yawnmoth

I'm trying to, via an applet, download the contents of a URL and
display them. To that end - I've written test.java (which follows).
Unfortunately, it doesn't work. After showing the "splash screen" for
a while, it doesn't output anything. Any ideas as to why?:

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class test extends Applet
{
public void start()
{
try
{
URL url = new URL("http://www.google.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();

BufferedReader text = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String output = "";

while ( text.ready() )
{
output+= text.readLine();
}

add(new Label(output));
}
catch (Exception e)
{
}
}
}
 
O

Oliver Wong

yawnmoth said:
I'm trying to, via an applet, download the contents of a URL and
display them. To that end - I've written test.java (which follows).
Unfortunately, it doesn't work. After showing the "splash screen" for
a while, it doesn't output anything. Any ideas as to why?:
[most of the code snipped]

Google explicitly blocks Java programs from connecting to it.

- Oliver
 
Y

yawnmoth

Oliver said:
Google explicitly blocks Java programs from connecting to it.
It doesn't seem to block this, when ran from the command line:

import java.net.*;
import java.io.*;

public class test
{
public static void main(String[] args)
{
try
{
URL url = new URL("http://www.google.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader text = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
while ( text.ready() )
{
System.out.println(text.readLine());
}
}
catch (Exception e)
{
}
}
}

I also changed www.google.com in the code I initially posted and the
only difference that made was that no splash screen was shown...
 
O

Oliver Wong

yawnmoth said:
I'm trying to, via an applet, download the contents of a URL and
display them. [most of code snipped]
URL url = new URL("http://www.google.com/");

Sorry, I read your message too fast. Another problem is that unsigned
applets cannot connect to any server except the one they were downloaded
from for security reasons. So to make this work as an applet, you'd have to
sign it.

- Oliver
 
R

Roland de Ruiter

yawnmoth said:
I'm trying to, via an applet, download the contents of a URL and
display them. To that end - I've written test.java (which follows).
Unfortunately, it doesn't work. After showing the "splash screen" for
a while, it doesn't output anything. Any ideas as to why?:
[most of the code snipped]

Google explicitly blocks Java programs from connecting to it.
The Google home page isn't, but a search URL is (e.g.
http://www.google.com/search?q=Java).

It can, however, easily be circumvented by supplying a "User-Agent"
request header with the UA string of one of the major browsers as its value.

import java.io.*;
import java.net.*;

public class GoogleAccess {

private static final String USER_AGENT
= "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.8.0.6) "
+ "Gecko/20060728 Firefox/1.5.0.6";

public static void main(String[] args) throws IOException {

URL url = new URL("http://www.google.com/search?q=Java");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.addRequestProperty("User-Agent", USER_AGENT);

String contentType = connection.getContentType();
String contentEncoding = connection.getContentEncoding();
int responseCode = connection.getResponseCode();
System.out.println("responseCode=" + responseCode + ";
contentType="
+ contentType + "; contentEncoding=" + contentEncoding);

InputStreamReader reader = new InputStreamReader(connection
.getInputStream(), contentEncoding == null ? "ISO-8859-1"
: contentEncoding);
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
reader.close();
connection.disconnect();
}
}
 
A

Andrew Thompson

yawnmoth said:
I'm trying to,

...whenever you 'try' something..
try
{
URL url = new URL("http://www.google.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

...make sure that when you 'catch' it
catch (Exception e)
{

// Do *not* ignore the exception.
e.printStackTrace();

...I think Oliver has spotted the underlying problem, but if
that stack trace was dumped to the console* it would provide
detailed information on both the nature and origin of the problem.

The point I am trying to impress is,
Don't ignore stackTraces - especially in broken code.

* The Java console - you know where to find it in your
browser, right?

And please stop posting 'tab's in code samples..

Andrew T.
 
V

vvk

hello,
actually untrusted applets can't contact the local DNS server to
locate ur url........

__Vasantha kumar
 
Y

yawnmoth

Oliver said:
yawnmoth said:
I'm trying to, via an applet, download the contents of a URL and
display them. [most of code snipped]
URL url = new URL("http://www.google.com/");

Sorry, I read your message too fast. Another problem is that unsigned
applets cannot connect to any server except the one they were downloaded
from for security reasons. So to make this work as an applet, you'd have to
sign it.
I just uploaded it to a webserver and changed the URL to that server
and it worked just fine. I guess that was indeed the problem - thanks!

That said... *.html files using JavaScript's XmlHttpRequest object can
access pages on any domain if the *.html file is being ran off of the
local filesystem (as opposed to from a domain / ip address). I guess
there is no such policy for Java apps? ie. unlike XmlHttpRequest,
applets being ran from c:\some\dir are still subject to that policy?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top