Java by API/javax.net/URL

Материал из Java эксперт
Версия от 14:35, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

new URL(String address)

/*
 * Output:
 * 
 * 
Protocol: http
Port: -1
Host: www.jexp.ru
File: 
Ext:http://www.jexp.ru
 */
import java.net.URL;
public class MainClass {
  public static void main(String args[]) throws Exception {
    URL hp = new URL("http://www.jexp.ru");
    System.out.println("Protocol: " + hp.getProtocol());
    System.out.println("Port: " + hp.getPort());
    System.out.println("Host: " + hp.getHost());
    System.out.println("File: " + hp.getFile());
    System.out.println("Ext:" + hp.toExternalForm());
  }
}





new URL(String protocol, String host, int port, String file)

/*
 * Output:
 * 
Date: 1145919603000
Type: text/html
Exp: 0
Last M: 1143834458000
Length: 208546
 */

import java.net.URL;
import java.net.URLConnection;
public class MainClass {
  public static void main(String args[]) throws Exception {
    int c;
    URL hp = new URL("http", "www.jexp.ru", 80, "/");
    URLConnection hpCon = hp.openConnection();
    System.out.println("Date: " + hpCon.getDate());
    System.out.println("Type: " + hpCon.getContentType());
    System.out.println("Exp: " + hpCon.getExpiration());
    System.out.println("Last M: " + hpCon.getLastModified());
    System.out.println("Length: " + hpCon.getContentLength());
  }
}





URL: getFile()

/*
 * Output:
 * 
 * 
Protocol: http
Port: -1
Host: www.jexp.ru
File: 
Ext:http://www.jexp.ru
 */
import java.net.URL;
public class MainClass {
  public static void main(String args[]) throws Exception {
    URL hp = new URL("http://www.jexp.ru");
    System.out.println("Protocol: " + hp.getProtocol());
    System.out.println("Port: " + hp.getPort());
    System.out.println("Host: " + hp.getHost());
    System.out.println("File: " + hp.getFile());
    System.out.println("Ext:" + hp.toExternalForm());
  }
}





URL: getHost()

/*
 * Output:
 * 
 * 
Protocol: http
Port: -1
Host: www.jexp.ru
File: 
Ext:http://www.jexp.ru
 */
import java.net.URL;
public class MainClass {
  public static void main(String args[]) throws Exception {
    URL hp = new URL("http://www.jexp.ru");
    System.out.println("Protocol: " + hp.getProtocol());
    System.out.println("Port: " + hp.getPort());
    System.out.println("Host: " + hp.getHost());
    System.out.println("File: " + hp.getFile());
    System.out.println("Ext:" + hp.toExternalForm());
  }
}





URL: getPort()

/*
 * Output:
 * 
 * 
Protocol: http
Port: -1
Host: www.jexp.ru
File: 
Ext:http://www.jexp.ru
 */
import java.net.URL;
public class MainClass {
  public static void main(String args[]) throws Exception {
    URL hp = new URL("http://www.jexp.ru");
    System.out.println("Protocol: " + hp.getProtocol());
    System.out.println("Port: " + hp.getPort());
    System.out.println("Host: " + hp.getHost());
    System.out.println("File: " + hp.getFile());
    System.out.println("Ext:" + hp.toExternalForm());
  }
}





URL: getProtocol()

/*
 * Output:
 * 
 * 
Protocol: http
Port: -1
Host: www.jexp.ru
File: 
Ext:http://www.jexp.ru
 */
import java.net.URL;
public class MainClass {
  public static void main(String args[]) throws Exception {
    URL hp = new URL("http://www.jexp.ru");
    System.out.println("Protocol: " + hp.getProtocol());
    System.out.println("Port: " + hp.getPort());
    System.out.println("Host: " + hp.getHost());
    System.out.println("File: " + hp.getFile());
    System.out.println("Ext:" + hp.toExternalForm());
  }
}





URL: openConnection()

/*
 * Output:
 * 
Date: 1145919603000
Type: text/html
Exp: 0
Last M: 1143834458000
Length: 208546
 */

import java.net.URL;
import java.net.URLConnection;
public class MainClass {
  public static void main(String args[]) throws Exception {
    int c;
    URL hp = new URL("http", "www.jexp.ru", 80, "/");
    URLConnection hpCon = hp.openConnection();
    System.out.println("Date: " + hpCon.getDate());
    System.out.println("Type: " + hpCon.getContentType());
    System.out.println("Exp: " + hpCon.getExpiration());
    System.out.println("Last M: " + hpCon.getLastModified());
    System.out.println("Length: " + hpCon.getContentLength());
  }
}





URL: toExternalForm()

/*
 * Output:
 * 
 * 
Protocol: http
Port: -1
Host: www.jexp.ru
File: 
Ext:http://www.jexp.ru
 */
import java.net.URL;
public class MainClass {
  public static void main(String args[]) throws Exception {
    URL hp = new URL("http://www.jexp.ru");
    System.out.println("Protocol: " + hp.getProtocol());
    System.out.println("Port: " + hp.getPort());
    System.out.println("Host: " + hp.getHost());
    System.out.println("File: " + hp.getFile());
    System.out.println("Ext:" + hp.toExternalForm());
  }
}