Communication connection for URLs---Java urlconnection class

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.