Java 具有URL子上下文的套接字连接

Java 具有URL子上下文的套接字连接,java,Java,我用下面的代码来阅读bbc网站的子上下文新闻,但它抛出了UnknownHostException。请给出任何提示 try { InetAddress addr = InetAddress.getByName("bbc.co.uk/news/"); int port = 80; // This constructor will block until the connection succeeds S

我用下面的代码来阅读bbc网站的子上下文新闻,但它抛出了UnknownHostException。请给出任何提示

try {
            InetAddress addr = InetAddress.getByName("bbc.co.uk/news/");
            int port = 80;

            // This constructor will block until the connection succeeds
            Socket socket = new Socket(addr, port);
        } catch (UnknownHostException e) {
            System.out.println("exception is"+e);
        } catch (IOException e) {
        }

InetAddress.getByName(…)
上的JavaDoc:

确定给定主机名称的主机的IP地址

bbc.co.uk/news/
中,主持人是
bbc.co.uk
,其中
news
是您已经说过的子文本

因此,将其更改为
InetAddress.getByName(“bbc.co.uk”)
,或者使用
URL
类,前提是您不必使用
Socket
新URL(“http://bbc.co.uk/news/)。openStream()

您的方法不正确。它应该只指定主机名

InetAddress addr = InetAddress.getByName("bbc.co.uk");

:谢谢你的回复。我必须在没有URL包帮助的情况下完成。:我只能使用套接字,不能使用URL包。