【问题标题】:Can't connect and read a web page [duplicate]无法连接和阅读网页[重复]
【发布时间】:2016-12-03 14:43:35
【问题描述】:

我正在尝试建立 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)

我在这里做错了什么。任何帮助表示赞赏。

【问题讨论】:

    标签: java


    【解决方案1】:

    您对HttpURLConnection 的用法有误。这是一个 connect() 的循环,它什么都不做

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

    这里提到了HttpURLConnection类的正确模式: https://developer.xamarin.com/api/type/Java.Net.HttpURLConnection/

    修改您的Connection() 如下所示将使其工作。

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

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      相关资源
      最近更新 更多