weixin_39181207
落单的大苞米
2017-09-02 23:41

UrlConnection和URL的getcontent的使用方法

  • java
  • 网络通信
 package com.newterm;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author Dam
 *未完成
 */
public class UrlConnectionDemo {
    public static void main(String[] args) throws Exception {
        URL baidu = new URL("http://www.sina.com/");
//      URLConnection bd = baidu.openConnection();
//      BufferedReader in = new BufferedReader(new InputStreamReader(bd.getInputStream()));
//      try {
//          Object content;
//      while((content=baidu.getContent())!=null)   
        BufferedReader in=new BufferedReader(new InputStreamReader(baidu.openStream()));
        try{
        Object content=baidu.getContent();
        System.out.println(content);
        } catch (Exception e) {
        System.out.println("internet error");
        }   


        in.close();

    }
}

结果出现
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3e3abc88

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐