六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 13|回复: 0

URLConnection类 示例

[复制链接]

升级  57%

113

主题

113

主题

113

主题

举人

Rank: 3Rank: 3

积分
371
 楼主| 发表于 2013-2-4 19:28:45 | 显示全部楼层 |阅读模式
//Demonstrate URLConnection.
import java.net.*;
import java.io.*;
import java.util.Date;
class URLConnectionDemo{
 public static void main(String[] args) throws Exception{
  int c;
  URL hp = new URL("http://www.osborne.com");
  URLConnection hpCon = hp.openConnection();
  System.out.println("Date: " + new Date(hpCon.getDate()));
  System.out.println("Content-Type: " + hpCon.getContentType());
  System.out.println("Expires: " + hpCon.getExpiration());
  System.out.println("Last-Modified: " + new Date(hpCon.getLastModified()));
  int len = hpCon.getContentLength();
  System.out.println("Content-Length: " + len );
  if(len>0){
   System.out.println("===== Content =====");
   InputStream input = hpCon.getInputStream();
   int i = len;
   while((c= input.read())!=-1 && (--i>0)){
    System.out.println((char)c);
   }
   input.close();
  }else{
   System.out.println("No Content Available.");
  }
 }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表