URL读取服务器端文档

>>URL读取服务器端文档实例
//URLConnection 类是所有在应用程序和 URL 之间创建通信链路的类的抽象超类。
//URLConnection 在获取 Web 服务器上的文档方面特别有用,但也可用于连接由 URL 标识的任何资源。
//演示如何用 URLConnection 来从服务器请求文档的样本代码。
import java.io.*;
import java.net.*;
public class URLClient
{
private URLConnection conn ;  //声明URL连接对象
//构造
public URLClient()
{}
//根据URL返回URL的文档内容
public String getDocument(String urlString)
{
   //实例化StrignBuffer对象,用于接收文档内容
  StringBuffer document = new StringBuffer();

  try{
   URL url = new URL(urlString); //根据URL地址,实例化URL对象
   conn = url.openConnection();  //打开URL对象的连接,返回URLConnection对象
   //根据URL连接对象,创建缓冲流读取器
   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
   String line = null;
   //如果读取的内容不为空,则继续读取
   while((line = reader.readLine())!=null)
   {
    document.append(line+"\n");
   }
   //关闭缓冲流对象
   reader.close();
  
  }
  catch(Exception e){
   e.printStackTrace();
  }
  return document.toString();
}
public static void main(String[] args)
{
   URLClient client = new URLClient();
   String baidu = client.getDocument(" http://www.baidu.com ");
   System.out.println(baidu);
}
}



>>牛人解说URL对象的工作原理
服务器请求一个文档 第 5 页(共6 页) getDocumentAt() 方法处理获取 Web 上的文档的实际工作:
public String getDocumentAt(String urlString) {    StringBuffer document = new StringBuffer();    try {        URL url = new URL(urlString);        URLConnection conn = url.openConnection();        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));        String line = null;        while ((line = reader.readLine()) != null)       document.append(line + "\n");        reader.close();    } catch (MalformedURLException e) {        System.out.println("Unable to connect to URL: " + urlString);    } catch (IOException e) {        System.out.println("IOException when connecting to URL: " + urlString);    }    return document.toString();}
getDocumentAt() 方法有一个 String 参数,该参数包含我们想获取的文档的 URL。我们在开始时创建一个 StringBuffer 来保存文档的行。然后我们用我们传进去的 urlString 创建一个新 URL。接着创建一个 URLConnection 并打开它:
URLConnection conn = url.openConnection();
一旦有了一个 URLConnection,我们就获取它的 InputStream 并包装进 InputStreamReader,然后我们又把 InputStreamReader 包装进 BufferedReader 以使我们能够读取想从服务器上获取的文档的行。在 Java 代码中处理套接字时,我们将经常使用这种包装技术,但我们不会总是详细讨论它。在我们继续往前讲之前,您应该熟悉它:
BufferedReader reader =     new BufferedReader(new InputStreamReader(conn.getInputStream()));
有了 BufferedReader,就使得我们能够容易地读取文档内容。我们在 while 循环中调用 reader 上的 readLine()
String line = null;while ((line = reader.readLine()) != null)    document.append(line + "\n");
readLine() 的调用将直至碰到一个从 InputStream 传入的行终止符(例如换行符)时才阻塞。如果没碰到,它将继续等待。只有当连接被关闭时,它才会返回 null。在这个案例中,一旦我们获取一个行(line),我们就把它连同一个换行符一起附加(append)到名为 documentStringBuffer 上。这保留了服务器端上读取的文档的格式。
我们在读完行之后关闭 BufferedReader
reader.close();
如果提供给 URL 构造器的 urlString 是无效的,那么将抛出 MalformedURLException。如果发生了别的错误,例如当从连接上获取 InputStream 时,那么将抛出 IOException

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值