Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1675001
  • 博文数量: 210
  • 博客积分: 10013
  • 博客等级: 上将
  • 技术积分: 2322
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 15:56
文章分类

全部博文(210)

文章存档

2011年(34)

2010年(121)

2009年(37)

2008年(18)

我的朋友

分类: Java

2009-08-20 10:35:37

package test;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.util.Date;
/**
 * URL测试程序
 * @author 静止的流水
 *
 */

public class GetUrlInfor
{
 private static String url;
 /**
  * 构造函数
  * @param s
  */

 public GetUrlInfor(String s)
 {
  url = s;
 }
 /**
  * 单行打印
  * @param s
  */

 private static void print(String s)
 {
  System.out.println(s);
 }
 /**
  * 打印函数
  * @throws Exception
  */

 public void printinfor()throws Exception
 {
  URL urlnew = new URL(url);
  print("协议 = "+urlnew.getProtocol());
  print("主机地址 = "+urlnew.getHost());
  print("文件名 = "+urlnew.getFile());
  print("端口 = "+urlnew.getPort());
  
  URLConnection newconnection = urlnew.openConnection();
  newconnection.connect();
  
  print(newconnection.getContentEncoding());
  print(newconnection.getContentType());
  print(String.valueOf((newconnection.getContentLength())));
  print(newconnection.getContent().toString());
  print(new Date(newconnection.getLastModified()).toString());
  
  if(newconnection instanceof HttpURLConnection)
  {
   HttpURLConnection h = (HttpURLConnection) newconnection;
   print(h.getRequestMethod());
   print(h.getResponseMessage());
   print(String.valueOf(h.getResponseCode()));
 
  }
 }
 /**
  * 主函数
  * @param args
  * @throws Exception
  */

 public static void main(String[] args)throws Exception
 {
  String url = "";
  GetUrlInfor geturlinfor = new GetUrlInfor(url);
  geturlinfor.printinfor();
 }
 
}

阅读(1295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~