java - URL类里面的方法openConnection方法报错
PHPz
PHPz 2017-04-18 10:29:37
[Java讨论组]

我按照教程来却发现总是报错;
代码如下
private String url; //请求的地址

public httpThread(String url) {
    this.url = url;
}
public String doGet() {
    URL httpUrl = null;
    try {
        httpUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    HttpURLConnection httpURLConnection;
    httpURLConnection = (HttpURLConnection) new httpUrl.openConnection();
    try {
        httpURLConnection.setRequestMethod("GET");      //请求get方法
    } catch (ProtocolException e) {
        e.printStackTrace();
    }
    httpURLConnection.setReadTimeout(5000);
    //接受返回来的数据
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    String str;
    StringBuffer sb = new StringBuffer();
    try {
        while ((str = reader.readLine()) != null) {
            sb.append(str);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
![图片描述][2]
PHPz
PHPz

学习是最好的投资!

全部回复(2)
天蓬老师

请问 这是什么教程?使用浏览器看看请求是否管用 有可能作者编写时可以访问但是后来不能了

黄舟
    public URLConnection openConnection() throws java.io.IOException {
        return handler.openConnection(this);
    }

这就是个方法你把new去掉啊

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号