URL的一段测试代码(转)

    xiaoxiao2021-12-14  15

    package test.sockettest;

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

    public class UrlTest {

     /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  try {   //1 创建一个对象   URL load = new URL("http://www.baidu.com");   //2 创建一个URLConnection   URLConnection conn = load.openConnection();   conn.connect();   InputStreamReader in = new InputStreamReader(conn.getInputStream());   BufferedReader data = new BufferedReader(in);   String line;   StringBuffer buf = new StringBuffer();      3 getInputStream取得流 这里用不为空来判断   while((line=data.readLine())!=null){    buf.append(line);   }   System.out.println("buf:"+buf);     } catch (MalformedURLException e) {   // TODO Auto-generated catch block   e.printStackTrace();  } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  } }

    }

     

    结果:

    <!doctype html><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>百度

    转载请注明原文地址: https://ju.6miu.com/read-964621.html

    最新回复(0)