在抛出:IllegalArgumentException在URLPermission jre8 [英] IllegalArgumentException at URLPermission in jre8

查看:1329
本文介绍了在抛出:IllegalArgumentException在URLPermission jre8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行code以下,

-in上JRE8一个Applet,就行了 con.getInputStream()它抛出异常

-in上JRE7或JRE6一个Applet的不会抛出。

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

当我删除行开头的 setRequestPropery ,它的不抛出上的任何JRE除外。

 的URLConnection CON =新的URL(ADRESS).openConnection();
        con.setDoOutput(真);
        con.setDoInput(真);
        con.setUseCaches(假);
        con.setRequestProperty(内容类型,
                应用程序/八位字节流);
        con.setRequestProperty(杂,无缓存);
        PrintStream的PS =新的PrintStream(con.getOutputStream());
        ps.println(测试);
        ps.close();
        在=新DataInputStream所(conn.getInputStream());

例外:

  java.lang.IllegalArgumentException异常:无效的字符串操作
在java.net.URLPermission.init(来源不明)
在java.net.URLPermission<&初始化GT;(未知来源)
在sun.net.www.protocol.http.HttpURLConnection.URLtoSocketPermission(来源不明)
在sun.net.www.protocol.http.HttpURLConnection.getOutputStream(来源不明)

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

你知道是什么原因导致的JRE8此异常?为什么只在一个小程序,而不是desktopapp。


解决方案

DEBUGIN在applet的code片段,显示参数动作传递给<一个HREF =htt​​p://docs.oracle.com/javase/8/docs/api/java/net/URLPermission.html相对=nofollow> URLPermission ,该新北京时间赢java8,具有价值 GET:编译:根据Javadoc中这样的说法是不正确的:


  

的操作字符串URLPermission的是方法的串联
  列表和请求头列表。这些是允许的列表
  申请方法和许可的许可请求头
  (分别)。 ':'字符的两列被用冒号隔开
  每个列表元素用逗号分隔。一些实例是:

 的帖子,获取,删除
     GET:X-美孚请求,X-BAR-请求
     POST,GET:头1,头2


和根据甲骨文jdk8的code:

  INT冒号= actions.indexOf(':');
如果(actions.lastIndexOf(':')=冒号!){
    抛出新抛出:IllegalArgumentException(无效的操作字符串);
}

上面的code预计,一个单一的结肠或没有。

要解决你需要在编译来去除结肠您的通话问题

  con.setRequestProperty(杂,无缓存);

运行你的代码片段简单JUnit测试不会引发这种异常,因为不调用 URLPermition 类。无论援引与否取决于在其中运行应用程序的环境。


  

请注意。根据使用的上下文中,某些请求方法和头可能在任何时候都被允许,和其他人可能不会在任何时候被允许。例如,HTTP协议处理程序可能会禁止某些头,比如内容长度由应用程序code被设置,无论现行的安全政策是否允许它。


如此看来,当一个applet的背景下一些permition进行了检查。

when i run the code below,

-in an Applet on JRE8, on the line con.getInputStream() it throws the exception

-in an Applet on JRE7 or JRE6 it does not throws.

-in a Desktop app on any JRE it does not throws.

when i remove the lines starts with setRequestPropery, it does not throws the exception on any 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)

In my applet, i am trying to open a connection and i need those request properties.

Do you know what causes this exception on JRE8? and why only in an applet and not desktopapp.

解决方案

Debugin your code snippet in an applet, shows that the parameter actions passed to URLPermission, which ist new win java8, has the value GET:pragma: which is not valid according to javadoc for that argument:

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"

and according to the code in oracle's jdk8:

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

The code above expects one single colon or none.

To solve the problem you need to remove the colon after pragma in your call

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

Running your snippet as simple junit test does not provoke this exception, because the URLPermition class is not invoked. Whether it invoked or not depends on the context where the application is running.

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.

So it seems, when in the context of an applet some permition checks are performed.

这篇关于在抛出:IllegalArgumentException在URLPermission jre8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆