java - jre8 中 URLPermission 的 IllegalArgumentException

标签 java applet httpurlconnection japplet illegalargumentexception

当我运行下面的代码时,

-在 JRE8 上的 Applet 中,在 con.getInputStream() 行中抛出异常

-在 JRE7 或 JRE6 上的 Applet 中它不会抛出。

-在任何 JRE 上的桌面应用程序中它不会抛出。

当我删除以 setRequestPropery 开头的行时,它不会抛出任何 JRE 上的异常。

        URLConnection con = new URL(adress).openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setUseCaches(false);
        con.setRequestProperty("Content-Type",
                "application/octet-stream");
        con.setRequestProperty("pragma:", "no-cache");
        PrintStream ps = new PrintStream(con.getOutputStream());
        ps.println("Test");
        ps.close();
        in = new DataInputStream(conn.getInputStream());

异常(exception):

java.lang.IllegalArgumentException: invalid actions string
at java.net.URLPermission.init(Unknown Source)
at java.net.URLPermission.<init>(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.URLtoSocketPermission(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)

在我的小程序中,我试图打开一个连接并且我需要那些请求属性。

您知道 JRE8 上出现此异常的原因是什么吗?以及为什么只在小程序中而不是桌面应用程序中。

最佳答案

在小程序中调试您的代码片段,显示参数 actions 传递给了 URLPermission ,这是新的 win java8,具有 GET:pragma: 的值,根据该参数的 javadoc,该值无效:

The actions string of a URLPermission is a concatenation of the method list and the request headers list. These are lists of the permitted request methods and permitted request headers of the permission (respectively). The two lists are separated by a colon ':' character and elements of each list are comma separated. Some examples are:

     "POST,GET,DELETE"
     "GET:X-Foo-Request,X-Bar-Request"
     "POST,GET:Header1,Header2"

根据oracle的jdk8中的代码:

int colon = actions.indexOf(':');
if (actions.lastIndexOf(':') != colon) {
    throw new IllegalArgumentException("invalid actions string");
}

上面的代码需要一个冒号或没有冒号。

要解决这个问题,您需要在调用中删除 pragma 之后的冒号

con.setRequestProperty("pragma", "no-cache");

将代码段作为简单的 junit 测试运行不会引发此异常,因为未调用 URLPermition 类。它是否被调用取决于应用程序运行的上下文。

Note. Depending on the context of use, some request methods and headers may be permitted at all times, and others may not be permitted at any time. For example, the HTTP protocol handler might disallow certain headers such as Content-Length from being set by application code, regardless of whether the security policy in force, permits it.

看起来,当在 applet 的上下文中执行一些许可检查时。

关于java - jre8 中 URLPermission 的 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466175/

相关文章:

java - 如何让图形消失?

java - 首先检查结果是否为空时如何避免冗余方法调用?

java - 使用 JAXB 或 XML DOM 对象时 Spring Web 服务返回不同的结果

java - IcedTea 1.8 版无法打开任何 JNLP 文件

java - 无法让计算器 JApplet GUI 以有序的方式显示按钮

从远程服务器下载文件到Android手机时出现java.io.eofException

java - 使用 HTTPClient 或 HttpUrlConnection?

java - 如何检查 HTTP 响应中的 HTML?

java - 在Java中使用方法和数组时出现“Cannot find symbol”错误

java - 导出一对纬度和经度之间的任意点纬度和经度