java.net.URI对主机部分中的特殊字符进行了扼流 [英] java.net.URI chokes on special characters in host part

查看:142
本文介绍了java.net.URI对主机部分中的特殊字符进行了扼流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的URI字符串:

I have a URI string like the following:


http://www.christlichepartei%F6sterreichs.at/steiermark/

我正在使用此字符串创建一个java.lang.URI实例并且它成功但是当我想要检索主机时它返回null。如果我完全如上所示输入它,Opera和Firefox也会阻塞此URL。但是,如果URI类无效,那么它应该抛出URISyntaxException吗?如何检测到URI是非法的?

I'm creating a java.lang.URI instance with this string and it succeeds but when I want to retrieve the host it returns null. Opera and Firefox also choke on this URL if I enter it exactly as shown above. But shouldn't the URI class throw a URISyntaxException if it is invalid? How can I detect that the URI is illegal then?

当我使用URLDecoder解码字符串时,它的行为也相同,产生

It also behaves the same when I decode the string using URLDecoder which yields


http:// www .christlicheparteiösterreichs.at/ steiermark /

http://www.christlicheparteiösterreichs.at/steiermark/

现在这已被Opera和Firefox接受,但java.net.URI仍然不喜欢它。我该如何处理这样的网址?

Now this is accepted by Opera and Firefox but java.net.URI still doesn't like it. How can I deal with such a URL?

谢谢

推荐答案

Java 6有 IDN 使用国际化域名的类。因此,以下产生带编码主机名的URI:

Java 6 has IDN class to work with internationalized domain names. So, the following produces URI with encoded hostname:

URI u = new URI("http://" + IDN.toASCII("www.christlicheparteiösterreichs.at") + "/steiermark/");

这篇关于java.net.URI对主机部分中的特殊字符进行了扼流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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