开发者

Is there a 'standard way' to parse about:blank URL's in Java?

开发者 https://www.devze.com 2023-02-10 19:34 出处:网络
With the following code fragment, if docurl = \"about:blank\", then I get a java.net.MalformedURLException thrown. I cant believe I am the first person to have to handle such urls so Im wondering if t

With the following code fragment, if docurl = "about:blank", then I get a java.net.MalformedURLException thrown. I cant believe I am the first person to have to handle such urls so Im wondering if there is a different api than java.net I can use?

        try {
            URL url;
            url = new URL(docurl);
            docuri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
        } catch (MalformedURLException e) {
                e.printStackTrace();
        } catch (URISyntaxException e) {
           开发者_开发技巧 e.printStackTrace();
        }


The issue is with the "about" protocol not having a valid handler.

The valid handlers for my jre6 installation are found in rt.jar

sun/net/www/protocol/ftp/Handler.class
sun/net/www/protocol/gopher/Handler.class
sun/net/www/protocol/http/NegotiateCallbackHandler.class
sun/net/www/protocol/mailto/Handler.class
sun/net/www/protocol/netdoc/Handler.class
sun/net/www/protocol/http/Handler.class
sun/net/www/protocol/jar/Handler.class
sun/net/www/protocol/file/Handler.class

You can make your own handler class by following the recipe at http://java.sun.com/developer/onlineTraining/protocolhandlers/


You can parse such URLs with java.net.URI.

Alternatively, I wrote a Java URL parsing library that offers a more general solution: galimatias.

0

精彩评论

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