Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

displaying URL contents via an applet

Thread view: 
yawnmoth - 15 Aug 2006 21:29 GMT
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)
        {
        }
    }
}
Oliver Wong - 15 Aug 2006 21:59 GMT
> 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]

> URL url = new URL("http://www.google.com/");

   Google explicitly blocks Java programs from connecting to it.

   - Oliver
yawnmoth - 15 Aug 2006 22:07 GMT
><snip>
> > URL url = new URL("http://www.google.com/");
>
>     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...
Roland de Ruiter - 15 Aug 2006 23:16 GMT
>> 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).
[quoted text clipped - 6 lines]
>
>    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();
    }
}

Signature

Regards,

Roland

Oliver Wong - 15 Aug 2006 22:20 GMT
> 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
yawnmoth - 16 Aug 2006 15:48 GMT
> > I'm trying to, via an applet, download the contents of a URL and
> > display them.
[quoted text clipped - 5 lines]
> 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?
Andrew Thompson - 16 Aug 2006 00:42 GMT
> 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.
vvk - 16 Aug 2006 08:34 GMT
hello,
actually untrusted applets can't contact the local DNS server to
locate ur url........

__Vasantha kumar
> 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).
[quoted text clipped - 32 lines]
>     }
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.