Public abstract class urlconnection{public URL GetURL () //Return the currently connected URL object public int Getcontentlength () //Returns the length of the resource file public String getContentType ()//returns the type of the resource file public long getlastmodified () // Returns the last modified date of the resource file}
The OpenConnection () method of the URL class can create a URLConnection object
Public URLConnection OpenConnection () throws IOException
☆ Internet Protocol (IP) address--inetaddress class
public class InetAddress implements serializable{
public static inetaddress Getbyname (String host)
public static inetaddress getbyaddress (String host, byte[] addr)
public static inetaddress Getlocalhost ()//return local Host
public string gethostaddress ()//return IP address string
Public String gethostname ()//Return host name
}
//code example
Package Cn.hncu.url;import Java.io.bufferedreader;import Java.io.inputstream;import java.io.InputStreamReader; Import Java.net.inetaddress;import java.net.url;import Java.net.urlconnection;import Java.util.Date;import Org.junit.test;public class Urldemo {@Testpublic void UrlDemo1 () {try {URL Source = new URL ("http://www.baidu.com"); Input Stream in = Source.openstream (); BufferedReader br = new BufferedReader (new InputStreamReader (in, "Utf-8")); String Line;while (line = Br.readline ())! = null) {System.out.println (line);}} catch (Exception e) {e.printstacktrace ();}} @Testpublic void Urlconnectiondemo () {try {URL source = new URL ("http://www.hncu.net:80"); URLConnection con = source.openconnection (); System.out.println (Con.geturl ()); System.out.println (Con.getcontentlength ()); System.out.println (Con.getcontenttype ()); System.out.println (New Date (Con.getlastmodified ()));} catch (Exception e) {e.printstacktrace ();}} @Testpublic void Inetaddressdemo () {try {inetaddress IP = inetaddress.getbyname("www.sina.com.cn");//inetaddress IP = inetaddress.getbyname ("58.47.143.5"); System.out.println (Ip.gethostaddress ()); System.out.println (Ip.gethostname ());} catch (Exception e) {e.printstacktrace ();}} @Testpublic void InetAddressDemo2 () {try {URL url = new URL ("http://www.hncu.net:80"); inetaddress IP = Inetaddress.getbyname (Url.gethost ()); System.out.println (Ip.gethostaddress ()); System.out.println (Ip.gethostname ());} catch (Exception e) {e.printstacktrace ();}}}
Communication connection for URLs---Java urlconnection class