为什么java的URL类不能识别某些协议? [英] why does java's URL class not recognize certain protocols?
问题描述
网址u =新网址(telnet://route-server.exodus.net);
URL u=new URL("telnet://route-server.exodus.net");
此行正在生成:
java.net.MalformedURLException:未知协议:telnet
java.net.MalformedURLException: unknown protocol: telnet
我遇到与以news://开头的其他网址类似的问题
And i encounter similar problems with other URLs that begin with "news://"
这些是从ODP中提取的URL,所以我不明白为什么会出现这种例外情况。
These are URLs extracted from ODP , so i dont understand why such exceptions arise..
推荐答案
问题
Java抛出 MalformedURLException
因为它找不到该协议的 URLStreamHandler
。检查构造函数的 javadocs 了解详细信息。
Java throws a MalformedURLException
because it couldn't find a URLStreamHandler
for that protocol. Check the javadocs of the constructors for the details.
摘要:
由于 URL
类有一个 openConnection
方法,URL类检查以确保Java知道如何打开正确协议的连接。如果没有该协议的 URLStreamHandler
,Java会拒绝创建 URL
,以便在您尝试调用<时避免失败code> openConnection 。
Since the URL
class has an openConnection
method, the URL class checks to make sure that Java knows how to open a connection of the correct protocol. Without a URLStreamHandler
for that protocol, Java refuses to create a URL
to save you from failure when you try to call openConnection
.
解决方案
您应该使用 <如果您不打算在Java中打开这些协议的连接,请使用code> URI 类。
You should probably be using the URI
class if you don't plan on opening a connection of those protocols in Java.
这篇关于为什么java的URL类不能识别某些协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!