I found an application that gets the source from a webpage. That worked fine. I tried converting it to an applet, and it works great in netbeans. The problem is when I try to run it in a browser, it does nothing. As you can see from the code, I tried to add a printwriter so I can just open the file and read what it got, but the applet doesn't even load that way. I read up on that and apparently I have to get the applet signed, and I have no idea what that is.
So then I tried to just get something printed out to a textbox. The applet loads fine, but when I hit the start button, it does nothing. Any ideas?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.HttpURLConnection;
import java.net.URL;
import java.applet.*;
import javax.swing.*;
import java.io.*;
public class main extends Applet{
JButton go = new JButton("Start!");
private String source;
JTextArea codeArea = new JTextArea(200,200);
//File outFile = new File("test.txt");
//FileOutputStream outFileStream = new FileOutputStream(outFile);
//PrintWriter outStream = new PrintWriter(outFileStream);
public main() throws IOException{
add(go);
add(codeArea);
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
URL pageURL = new URL("www.google.com");
HttpURLConnection urlConnection = (HttpURLConnection) pageURL.openConnection();
int respCode = urlConnection.getResponseCode();
String response = urlConnection.getResponseMessage();
//codeArea.setText("HTTP/1.x " + respCode + " " + response + "\n");
int count = 1;
while (true) {
String header = urlConnection.getHeaderField(count);
String key = urlConnection.getHeaderFieldKey(count);
if (header == null || key == null) {
break;
}
//codeArea.append(urlConnection.getHeaderFieldKey(count) + ": " + header + "\n");
count++;
}
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read()) != -1) {
source = String.valueOf((char) c);
System.out.print(source]);
codeArea.append(source);
//outStream.print(source);
//System.out.print(source);
}//outStream.close();
//codeArea.setCaretPosition(1);
} catch (Exception ee) {}
}
});
}
}