Java by API/java.net/URL — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:17, 31 мая 2010
Содержание
new URL(String spec) throws MalformedURLException
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] argv) throws Exception {
URLConnection conn = new URL("http://www.yourserver.ru").openConnection();
conn.setDoInput(true);
conn.setRequestProperty("Authorization", "asdfasdf");
conn.connect();
InputStream in = conn.getInputStream();
}
}
URL: getAuthority()
/*
URL is http://www.jexp.ru
authority is www.jexp.ru
path is
default port is 80
query is null
ref is null
*/
import java.io.IOException;
import java.net.URL;
public class MainClass {
public static void main(String[] args) {
try {
URL url = new URL("http://www.jexp.ru");
System.out.println("URL is " + url.toString());
System.out.println("authority is " + url.getAuthority());
System.out.println("path is " + url.getPath());
System.out.println("default port is " + url.getDefaultPort());
System.out.println("query is " + url.getQuery());
System.out.println("ref is " + url.getRef());
} catch (IOException e) {
e.printStackTrace();
}
}
}
URL: getDefaultPort()
/*
URL is http://www.jexp.ru
authority is www.jexp.ru
path is
default port is 80
query is null
ref is null
*/
import java.io.IOException;
import java.net.URL;
public class MainClass {
public static void main(String[] args) {
try {
URL url = new URL("http://www.jexp.ru");
System.out.println("URL is " + url.toString());
System.out.println("authority is " + url.getAuthority());
System.out.println("path is " + url.getPath());
System.out.println("default port is " + url.getDefaultPort());
System.out.println("query is " + url.getQuery());
System.out.println("ref is " + url.getRef());
} catch (IOException e) {
e.printStackTrace();
}
}
}
URL.getPath()
/*
URL is http://www.jexp.ru
authority is www.jexp.ru
path is
default port is 80
query is null
ref is null
*/
import java.io.IOException;
import java.net.URL;
public class MainClass {
public static void main(String[] args) {
try {
URL url = new URL("http://www.jexp.ru");
System.out.println("URL is " + url.toString());
System.out.println("authority is " + url.getAuthority());
System.out.println("path is " + url.getPath());
System.out.println("default port is " + url.getDefaultPort());
System.out.println("query is " + url.getQuery());
System.out.println("ref is " + url.getRef());
} catch (IOException e) {
e.printStackTrace();
}
}
}
URL: getQuery()
/*
URL is http://www.jexp.ru
authority is www.jexp.ru
path is
default port is 80
query is null
ref is null
*/
import java.io.IOException;
import java.net.URL;
public class MainClass {
public static void main(String[] args) {
try {
URL url = new URL("http://www.jexp.ru");
System.out.println("URL is " + url.toString());
System.out.println("authority is " + url.getAuthority());
System.out.println("path is " + url.getPath());
System.out.println("default port is " + url.getDefaultPort());
System.out.println("query is " + url.getQuery());
System.out.println("ref is " + url.getRef());
} catch (IOException e) {
e.printStackTrace();
}
}
}
URL: getRef()
/*
URL is http://www.jexp.ru
authority is www.jexp.ru
path is
default port is 80
query is null
ref is null
*/
import java.io.IOException;
import java.net.URL;
public class MainClass {
public static void main(String[] args) {
try {
URL url = new URL("http://www.jexp.ru");
System.out.println("URL is " + url.toString());
System.out.println("authority is " + url.getAuthority());
System.out.println("path is " + url.getPath());
System.out.println("default port is " + url.getDefaultPort());
System.out.println("query is " + url.getQuery());
System.out.println("ref is " + url.getRef());
} catch (IOException e) {
e.printStackTrace();
}
}
}
URL: openConnection()
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
Date today = new Date();
long millisecondsPerDay = 24 * 60 * 60 * 1000;
URL u = new URL("http://www.jexp.ru");
URLConnection uc = u.openConnection();
uc.setIfModifiedSince((new Date(today.getTime() - millisecondsPerDay)).getTime());
InputStream in = new BufferedInputStream(uc.getInputStream());
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read()) != -1) {
System.out.print((char) c);
}
}
}
URL: openStream()
/*
* Output:
*
*/
import java.io.InputStream;
import java.net.URL;
public class MainClass {
public static void main(String args[]) {
try {
URL url = new URL("http://www.jexp.ru");
// Obtain output stream
InputStream is = url.openStream();
// Read and display data from url
byte buffer[] = new byte[1024];
int i;
while ((i = is.read(buffer)) != -1) {
System.out.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}