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;
}
}
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;
}
}