Displaying Retrieved Data on Applet using HttpConnetion

F

farazkazmi

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

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(){


try{
Text = GetRequest();
setBackground(Color.gray);
}catch(Exception e)
{

}

}

public void start() {
System.out.println("starting...");
if (runner == null)
;
{
runner = new Thread(this);
runner.start();
}

}

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

public void stop() {
System.out.println("stopping...");
if (runner != null) {
runner.stop();
runner = null;
}
}

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

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

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

public void paint(Graphics g) {
g.setColor(Color.blue);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
g.setColor(Color.white);
System.out.println("\n********"+Text+"*************\n");
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
.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
System.out.println("\n");
// PROXY for My LAN.
Properties props = System.getProperties();
props.put("http.proxyHost", "SERVER");
props.put("http.proxyPort", "8080");
httpConnection.connect();


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

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

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

message.append((char) ch);

}


httpConnection.disconnect();
return outputMessage;
}

}
 
R

Robert Klemme

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

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..

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

robert
 
M

Matt Humphrey

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

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..

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.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
A

Andrew Thompson

...
Does the applet throw an exception?
Probably..

.....
Is there any output on the Java Console?
...not.

<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.

HTH
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top