猴王牌 2013-04-28 14:55 采纳率: 50%
浏览 480
已采纳

HttpURLConnection 抓去页面 返回始终是 Null

[code="java"]

package util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import sun.net.www.protocol.http.HttpURLConnection;

public class TestURL {

public static void main(String[] args) {
    try {
        URL url = new URL("http://www.baidu.com");
        HttpURLConnection urlc =(HttpURLConnection) url.openConnection();
        //urlc.getContent();
        urlc.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream(),"UTF-8"));
        StringBuffer sb = new StringBuffer();
        while (br.readLine() != null) {
            sb.append(br.readLine());
        }
        System.out.println(sb.toString());
        br.close();
        urlc.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

[/code]

  • 写回答

1条回答 默认 最新

  • iteye_5246 2013-04-28 15:08
    关注

    哥们送你个demo,你慢慢看吧,貌似你:
    构建
    [code="java"]BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream(),"UTF-8")); [/code]
    的时候应该这样:
    [code="java"]reader = new BufferedReader(new InputStreamReader(connection
    .getInputStream(),"UTF-8")); [/code]

    下面是demo:
    [code="java"]
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;

    public class TestUrlConnection {

    public static void main(String[] args) {
        BufferedReader reader = null;
        HttpURLConnection connection = null;
        try {
            // 新建一个URL对象,指定到请求的url.
            URL url = new URL("http://www.baidu.com");
            connection = (HttpURLConnection) url.openConnection();
    
            reader = new BufferedReader(new InputStreamReader(connection
                    .getInputStream(),"UTF-8")); 
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection.disconnect();
        }
    }
    

    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!