Java Can';t连接并阅读网页

Java Can';t连接并阅读网页,java,Java,我正在尝试建立http连接并读取HTML页面的内容。这是我用来连接页面的类 public class Connection { private URL url; private HttpURLConnection httpURLConnection; public Connection(String url) throws MalformedURLException { this.url = new URL(url); this.h

我正在尝试建立http连接并读取HTML页面的内容。这是我用来连接页面的类

public class Connection {

    private URL url;
    private HttpURLConnection httpURLConnection;


    public Connection(String url) throws MalformedURLException {

        this.url = new URL(url);
        this.httpURLConnection = new HttpURLConnection(this.url) {
            @Override
            public void connect() throws IOException {

                httpURLConnection.connect();
                httpURLConnection.setReadTimeout(15000);
                httpURLConnection.setConnectTimeout(15000);
                httpURLConnection.setUseCaches(false);
                HttpURLConnection.setFollowRedirects(true);

            }

            @Override
            public void disconnect() {
                httpURLConnection.disconnect();
            }

            @Override
            public boolean usingProxy() {
                return false;
            }
        };
    }

    public String parse() throws IOException {
        InputStream is = httpURLConnection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while ((line = rd.readLine()) != null)
        {
            response.append(line);
            response.append('\n');
        }
        rd.close();
        return response.toString();
    }
}
这是呼叫代码

public static void main(String[] args) {
    // write your code here
        String url = "https://www.buffalo.edu";
        Connection c;
        String str = null;
        try {
            c = new Connection(url);
            str = c.parse();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(str);
    }
我得到以下例外

 null
java.net.UnknownServiceException: protocol doesn't support input
    at java.net.URLConnection.getInputStream(URLConnection.java:830)
    at io.soumasish.connections.Connection.parse(Connection.java:47)
    at io.soumasish.App.main(App.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我做错了什么。非常感谢您的帮助。

关于此线程的相同问题,这里有一个很好的解释,解释了为什么失败()


这里也引用了这本书()

您错误地使用了
HttpURLConnection
。这是一个
connect()
的循环,它什么也不做

this.httpURLConnection = new HttpURLConnection(this.url) {
    @Override
    public void connect() throws IOException {
       httpURLConnection.connect();
       ...
这里提到了HttpURLConnection类的正确模式:

修改您的
连接()
如下将使其正常工作

public Connection(String url) throws IOException {
    this.url = new URL(url);
    this.httpURLConnection = (HttpURLConnection) this.url.openConnection();
}