Displaying Retrieved Data on Applet using HttpConnetion

Discussion in 'Java' started by farazkazmi, Sep 12, 2005.

  1. farazkazmi

    farazkazmi Guest

    I m trying to connect to yahoo.com and show its header fields on an

    The Problem i m having is that when i use any IDE to run this code, it
    is working fine. but when i use class file in html code, The applet is
    loaded but nothing is displayed on Applet.
    Plz Tell me what is that prob..

    I am using this code./

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Properties;
    import java.util.Vector;

    * @author Kazmi
    * To change the template for this generated type comment go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    public class AppletTest extends Applet implements Runnable {

    int count = 190;

    String Text = "";

    Vector vec = new Vector();

    Thread runner;

    private String head = null;

    public void init(){

    Text = GetRequest();
    }catch(Exception e)



    public void start() {
    if (runner == null)
    runner = new Thread(this);


    public void run() {
    while (true) {
    count = count - 4;
    if (count < 20) {
    count = 190;
    try {
    } catch (InterruptedException e) {

    public void stop() {
    if (runner != null) {
    runner = null;

    public void repaint(Graphics g) throws MalformedURLException {
    g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

    g.drawString(Text, 10, count + 4);

    public void destroy() {
    System.out.println("preparing to unload...");

    public void paint(Graphics g) {
    g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
    g.drawString(Text, 10, count);


    public String GetRequest() throws IOException {

    String urlString = "http://www.google.com";
    DataInputStream dis = null;
    StringBuffer message = new StringBuffer();
    String outputMessage = new String();
    URL url = new URL(urlString);
    HttpURLConnection httpConnection = (HttpURLConnection) url
    // PROXY for My LAN.
    Properties props = System.getProperties();
    props.put("http.proxyHost", "SERVER");
    props.put("http.proxyPort", "8080");

    String headerName = httpConnection.getHeaderFieldKey(1);
    String headerValue = httpConnection.getHeaderField(1);
    outputMessage = headerName + " - "+ headerValue;

    System.out.println("\n" + headerName + " = " + headerValue);

    dis = new DataInputStream(httpConnection.getInputStream());
    int ch;
    while ((ch = dis.read()) != -1) {

    message.append((char) ch);


    return outputMessage;

    farazkazmi, Sep 12, 2005
    1. Advertisements

  2. Applets are only allowed to connect back to the site they were loaded from
    unless they are signed. This is a security feature.

    Robert Klemme, Sep 12, 2005
    1. Advertisements

  3. Does the applet throw an exception? Is there any output on the Java Console?

    Are you aware that applets in a browser (sandbox) are restricted to being
    able to contact only the server from which they came? I would expect your
    applet to throw a SecurityException when trying to reach yahoo. For this to
    work you applet must either be signed and the user must grant it permission,
    or the user must modify the security policy file of their JVM or you must
    proxy the request on your web server. (I don't think your HTTP proxy code
    will perform that function.) Or use some other technology (e.g. Java Web
    Start) instead of applets.

    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Sep 12, 2005
  4. <http://www.physci.org/codes/javafaq.jsp#stacktrace>

    Read the entire FAQ - applets are mentioned often, as
    is security and applets.

    read the group as well, there are a number of recent
    discussions of applet signing.

    Before you attempt this in an applet, I recommend you
    try it in an applicatopn. There are a number of major
    sites that reject standard 'get's made by Java code.

    Andrew Thompson, Sep 12, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.