1 / 16
文档名称:

网络编程.doc

格式:doc   大小:90KB   页数:16页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

网络编程.doc

上传人:xunlai783 2018/7/15 文件大小:90 KB

下载得到文件列表

网络编程.doc

相关文档

文档介绍

文档介绍:网络编程
获取URL的信息
(1)URL格式是由三部分组成:
协议(服务方式) + :// + IP地址或者域名(有时包括端口号) + / + 资源在主机的具体地址(目录和文件名)
(2).URL类描述了一个URL,其中很多get方法;
(3)URL类中的openConnection方法打开URL,得到一个URLConnection,提供URL资源的信息:内容类型、内容长度、编码方式、最后修改日期等等;
(4) 如果URL协议时HTTP,还可以获得请求的方法、响应消息、响应代码;
例子:
import ;
import .HttpURLConnection;
import .URL;
import .URLConnection;
import ;
public class GetURLInfo {
public static void printInfo(URL url)throws IOException{
//URL基本信息
(" File : " + ());
(" Protocol :" + ());
(" Host : " + ());
(" Port : " + ());
(" Path :" + ());
//获取URLConnection对象
URLConnection c = () ;
() ;
//显示信息
(" Content Type : " + ());
("ContentEncoding:"+());
(" Content Length : " + ());
(" Date :" + ()); ("LastModified:"+newDate(()));
(" Expiration: " + new Date(()));
//如果是HTTP连接,提供更丰富的信息
if(c instanceof HttpURLConnection){
HttpURLConnection h = (HttpURLConnection)c ;
(" Request Method: " + ());
(" Response Message : " + ());
(" Response Code : " + ());
}
}
public static void main(String args[]){
try{
String urlStr = ":80/" ;
URL url = new URL(urlStr);
printInfo(url) ;
}catch( Exception e){
() ;
}
}
}
运行结果:
File : /
Protocol :http
Host :
Port : 80
Path :/
Content Type : text/html
Content Encoding : null
Content Length : 1015
Date :1272806350000
LastModified : Mon Jun 01 11:01:56 CST 2009
Expiration: Thu Jan 01 08:00:00 CST 1970
Request Method: GET