开发者

Request URL in Javascript, Fetch URL Content using Java Applet, return to Javascript?

开发者 https://www.devze.com 2023-02-05 12:39 出处:网络
I\'m in the process of making a little experiment, it grabs a YouTube page, and returns the highest quality MP4 link, then plays this in a HTML 5 Video element.

I'm in the process of making a little experiment, it grabs a YouTube page, and returns the highest quality MP4 link, then plays this in a HTML 5 Video element.


Now I was using PHP with cURL to get the URL content (YouTube), but that only works on my local server (MP4 link is locked to IP address). I can't think of any other way to get the page content due to cross domain rules except a Java applet.

So I've built a Java applet that should return the content of a URL.

Java

import java.applet.Applet;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
//import java.net.SocketPermission;
//import java.net.URLDecoder;
//import java.net.URLEncoder;
//import java.util.Locale;
//import netscape.javascript.JSObject;

public class URLFetcherabc extends Applet
{
public String URLContent = "Currently empty";

 public void init()
 {
    System.out.println("Ready to recieve URL");
 }
 public void paint(Graphics g) {
       g.drawString("Java loaded.", 0, 10);
 }
  public void sta开发者_运维百科rt()
  {
       String container = getURL("http://www.youtube.com/watch?v=LQ0AFriC7ZM&Bingbv", "GET"); //<-- This works
       System.out.println(container); //<-- This works
       URLContent = container;
  }
  public void bingo()
    {
      String container = getURL("http://www.youtube.com/watch?v=LQ0AFriC7ZM&Bingbv", "GET"); 
       System.out.println(container); 
      URLContent = container;
  }

  public String getURL(String url, String httpMethod)
  {
    try
    {
      URL u = new URL(url);
      HttpURLConnection conn = (HttpURLConnection)u.openConnection();
      conn.setRequestMethod(httpMethod);
      conn.setRequestProperty("User-Agent", "");
      InputStream is = conn.getInputStream();
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      for (int bytesRead = 0; (bytesRead = is.read(buffer)) != -1; ) {
        output.write(buffer, 0, bytesRead);
      }
      URLContent = output.toString();
      return URLContent;
    } catch (Exception e) {
        System.out.println(e);
    }return null;
  }


}

Now I've got my applet on my page, if I run the method getURL in start() it will return the URL content correctly, but, if I call the getURL method from Javascript like this

Javascript

var URLFetcherApplet = document.getElementById('URLFetcher');
  URLFetcherApplet.bingo();

Which calls Bingo() in Java then I get an error in the Java console. "java.security.AccessControlException: access denied (java.net.SocketPermission www.youtube.com:80 connect,resolve) null"

So if I run the same method on start(), then it works, but if I try and access it through Javascript it fails, any ideas as to why its doing this?

Thanks


One thing for sure: you have an empty catch block, so you are swallowing any exceptions, hiding them. Since you say the function is returning null, the most probable scenario is that an exception happens somewhere inside the try-catch block. Change the code to:

} catch (Exception e) {
    throw new RuntimeException(e);
}

and see what comes up on the java console.

Edit: If you are not ready to listen to good advice, don't ask questions. I am 99.999% sure that the fault is indeed in your java code (no matter what you say in the comment) and that adding a proper error reporting will give you the real answer in an instant. Add the throw new RuntimeException(e) and look at the console.

My bet is that you will see some kind of "unauthorized operation" error, because probably your applet is not signed (at least you did not write anything about it) and therefore same origin policy still applies (if it didn't apply for unsigned applets, Java would be one great gaping security hole).

If it turns out I am right, I hope you accept my answer even if you consider me rude :-)

Second Edit: I checked your code (pasted into Netbeans, little edit, compiled) and it works perfectly (firefox, explorer - both on windows) AND your symptoms seem to shift (you either say it returns null, or that it returns undefined, or that you are not able to call it at all) - I've got one last idea. A common mistake people make is to call objects (like applets or flash) before they are really loaded. At what stage are you executing your javascript? Are you sure the applet is loaded? What if you add a timeout (a couple of seconds) or execute the function from an "onclick" on some button? does anything change? What browser are you using? what is your javascript? what operating system? which plugin version? Unless we can reproduce the problem, we cannot help.

Third Edit: (after some experiments by question's author); wrap the javascript call into a PriviledgedAction, for example like this:

public String callFromJs(final String url, final String method){

return AccessController.doPrivileged(new PriviledgetAction<String>(){
    public String run(){
        return getUrl(url, method);
    }
});
}

and don't swallow exceptions :-)


//  get the XML object
function get_xml_object() {
    if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
    else if (window.XMLHttpRequest) return new XMLHttpRequest();
    else {
        alert('Your browser does not support AJAX.');
        return null;
    }
}

//  fetch url
function fetch_url(whichurl) {
    var myobj=get_xml_object();
    myobj.open("GET",whichurl,false);
    myobj.send();
    return myobj.responseText;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消