- 已编辑
开始学习简单而基本的网络命令了,前天通过TCP协议建立了一个socket,在本机上让两个thread之间传输信息,感觉非常好玩,有种第二次以严肃姿态进入网络时代的感觉?
今天练习的是访问某个URL,以Yahoo为例,代码是书上的:
class YahooURL {
public static void main(String[] args) throws Exception{
int c;
java.net.URL yahoo= new java.net.URL("http://www.yahoo.com");
URLConnection con = yahoo.openConnection();
java.io.InputStream in = yahoo.openStream();
while ((c=in.read()) != -1)
System.out.print((char) c);
System.out.println("Content-type: "+con.getContentType());
System.out.println("Content Encoding: "+con.getContentEncoding());
System.out.println("Content-length: "+con.getContentLength());
System.out.println("Last-modified: "+new Date(con.getLastModified()));
System.out.println("Date: "+new Date(con.getDate()));
System.out.println("Expires: "+con.getExpiration());
System.out.println("Connection timeout: "+con.getConnectTimeout());
}
}
疑问/有趣之处:
- URL信息读取的开始,当in.read()不为-1时,输出该数字代表的character,输出结果是redirect。(见图)
为什么这么做书上没有解释,我试图修改-1到其他数值,比如0,33,100,114,其中0、33等ASCII对应不再redirect中任何字母的数值导致程序输出大量方框而且不断持续,而116 (t)、114 (r)、105 (i)等数值则导致“redirect”缩短到该数值对应字母出现的第一个位置前,比如116代表t,则输出“redirec”,而114代表r是第一个字符则没有输出。
该黑箱探索获得的猜想是:URL连接建立之初存在大量被接收成 -1 值的数据,在这个数字噪音背景中有 “redirect”。
实际情况是什么呢?
Last-modified 是1970年的1月1日(格林威治0时),经查询这是UNIX时间的开始。
Content-coding、Content-Length等尚不清楚具体含义,这些是不是HTTP Header的内容?