Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Small Applet Problem (http://www.programmingforums.org/showthread.php?t=13423)

grimpirate Jun 25th, 2007 3:51 PM

Small Applet Problem
 
I'm trying to create an applet which retrieves information from a foreign url. I'm not entirely sure that this is even allowable however, because my code generates the following exception:
:

exception: java.lang.RuntimeException: java.security.AccessControlException: access denied (java.net.SocketPermission cache.pando.com:80 connect,resolve).
And here's the code for the applet:
:

import java.awt.*;
import javax.swing.*;

import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class URLGet extends JApplet
{
    private String input;

    public void start()
    {
        try
        {
            URL u = new URL("http://cache.pando.com/soapservices/SendToWeb?action=info&format=json&id=5AEDE982393976A10050F2EC7C20C3C5EFDE0BBB&key=C51E9F2767B6747A9C9841AF7EEB9CC0E967D5B37CEC05B8C9DF310A03958AD2");
            URLConnection connection = u.openConnection();
           
            HttpURLConnection httpConnection = (HttpURLConnection)connection;
            int code = httpConnection.getResponseCode();
           
            if(code != HttpURLConnection.HTTP_OK)
            {
                throw new MalformedURLException("HTTP_OK failed");
            }
           
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
       
            boolean done = false;
            input = "";
            while(!done)
            {
                input += reader.readLine();
                if(input == null) done = true;
            }
        }
        catch(MalformedURLException u)
        {
            input = "MalformedURLException";
        }
        catch(IOException e)
        {
            input = "IOException";
        }
    }

    public void paint(Graphics g)
    {
        g.setColor(Color.white);
        g.fillRect(0, 0, 200, 100);
        g.setColor(Color.black);
        g.drawString(input, 20, 20);
       
    }
}


lectricpharaoh Jun 25th, 2007 7:08 PM

There was a similar "can't do this in Java" thread a short time back, and andro posted a nice link in response. See post #5 in this thread.

The short answer: you can't open a socket connection to a foreign site from the applet. If you're able to implement server-side code, you can always have the server do it (and if possible, cache results to reduce overhead).

[edit] Regarding your other post, it seems server-side code isn't viable, either. Hate to say it, but it sounds like you're screwed. Have you considered switching your web hosting? [/edit]

grimpirate Jun 25th, 2007 10:08 PM

yeah seems to be that switching web hosting is the only way to go. I was thinking Flash to work with XML, but flash has the same limitation. It can only read XML files on the domain. I may just have to get users to paste the actual content of the URL as opposed to trying to get my script to do all the work for them. I could use the javascript api, but I don't like to use javascript at all since I usually surf with javascript disabled.


All times are GMT -5. The time now is 2:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC