JOHANNES_NYÅTT
JOHANNES_NYÅTT

Reputation: 3425

Get page's html source using a Java applet

I know in scripting languages like Python that this is possible but I know that Java applets can't access other servers other than their own.

I don't know/think I can get this applet signed. Is there a way to use PHP to do what I want to accomplish?

I also know that this code will go to google.com

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;

public class tesURL extends Applet implements ActionListener{

  public void init(){
  String link_Text = "google";
  Button b = new Button(link_Text);
  b.addActionListener(this);
  add(b);
  }

  public void actionPerformed(ActionEvent ae){
  //get the button label
  Button source = (Button)ae.getSource();

  String link = "http://www."+source.getLabel()+".com";
  try
  {
  AppletContext a = getAppletContext();
  URL u = new URL(link);
//  a.showDocument(u,"_blank");
//  _blank to open page in new window  
  a.showDocument(u,"_self");
  }
  catch (MalformedURLException e){
  System.out.println(e.getMessage());
  }
  }
}

That is assuming that source.getLabel() is "google"

But how would I get the source html of that page?

The source html is dynamic and is updated every few seconds or miliseconds. But, the html is also updated, so I can still read the dynamic content directly from the html. I already did this in vb.net, but now I need to port it to Java, but I can't figure out how to access a page's html source; that's why I'm asking.

Upvotes: 0

Views: 259

Answers (1)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

AppletContext.showDocument opens a page in the browser, much like a hyperlink in HTML or a similar call in JavaScript would do. Under the Same Origin Policy you will not have access to this page if it is from a different site, even if the page is in an iframe.

Some sites may have a crossdomain.xml policy file that allows access if you were to read the contents of the java.net.URL directly. However, www.google.com appears to be using a restricted form that I don't believe is currently supported by the Java PlugIn.

Someone will probably suggest signing your applet, which turns off the "sandbox" security feature of Java. You would then need to persuade your users to trust your ability to publish safe signed code.

Upvotes: 1

Related Questions