📄 urlconnectiondemo.java
字号:
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.util.Date;
public class URLConnectionDemo
{
public static void main(String[] args)
{
try
{
int n;
//使用指定的URL:"http://www.yahoo.com.cn",来创建URL类对象url
URL url = new URL("http://www.yahoo.com.cn");
//使用url对象的openConnection()方法,来获取URLConnection类的对象
URLConnection urlConn = url.openConnection();
//用于创建输入流,用于获取指定url上资源文件的信息
InputStream in = urlConn.getInputStream();
//用于获取资源文件内容
System.out.println("The content of \"http://www.yahoo.com.cn\" is:\n");
while((n = in.read())!=-1)
{
char c = (char)n;
System.out.print(c);
}
//使用urlConn对象的getContentType()方法获取资源文件的类型,并显示
System.out.println("ContentType :"+urlConn.getContentType());
//使用urlConn对象的getContentLength()方法获取资源文件的长度,并显示
System.out.println("ContentLength :"+urlConn.getContentLength());
//使用urlConn对象的getDate()方法获取资源文件创建的时间,并显示
System.out.println("Date :"+new Date(urlConn.getDate()));
//使用urlConn对象的getLastModified()方法获取资源文件最后一次被修改的时间,并显示
System.out.println("ContentEncoding :"+new Date(urlConn.getLastModified()));
}
catch (MalformedURLException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -