[13] socket.pdf

(107 KB) Pobierz
QPrint
Klasa URL
Sockety
URL-e w Javie Î tworzenie
z
z
URL
umk =
= new
new URL("http://
URL("http://www
www.uni
uni.torun
torun.pl
pl");
");
URL (URL
URL (URL baseURL
baseURL,
, String relativeURL
String relativeURL)
z z
cc
1
URL
URL umk
umk
==
new
URL("http://
www
uni
torun
ppl
URL (URL
baseURL
,,
String relativeURL
648092962.020.png 648092962.021.png 648092962.022.png
URL-e w Javie Î wyjĢtki
zzc
MalformedURLExceptionzz
z z
try {
URL myURL
myURL =
= new
new URL(. . .)
URL(. . .)
} catch
catch (MalformedURLException
MalformedURLException e) {..}
e) {..}
URL-e Î analiza skþadowych
c
zcz z
c zcz
zcz c
zc
zcz
zcz c
z zz
2
try
URL
myURL
==
new
}}
}
catch
MalformedURLException
648092962.023.png 648092962.001.png 648092962.002.png 648092962.003.png 648092962.004.png
Czytanie z adresu URL
import java
java.net.*;
.net.*;
import java
java.io
io.*;
.*;
public class URLReader {
public static void main
public static void main(String
String[]
[] args
args)
) throws
throws
Exception {
URL y =
new
URL("http://
new
URL("http://www
www.yahoo
yahoo.com
com/");
/");
BufferedReader in =
new BufferedReader(
new InputStreamReader
= new BufferedReader
new BufferedReader
new InputStreamReader(y.
(y.openStream
openStream()));
()));
String inputLine;
while
while ((
((inputLine
inputLine = in.
= in.readLine
readLine()) !=
()) != null
null)
System.out
out.println
println(inputLine
inputLine);
);
in.close
close();
();
}}
URLConnection
c
ccz c czczz
zczzz
c
zzzccz
cczcz
zzcc
zcc
try
URL yahoo
yahoo =
= new
new URL("http://
URL("http://www
www.yahoo
yahoo.com
com/");
/");
yahoo.openConnection
openConnection();
();
} catch
catch (MalformedURLException
MalformedURLException e) {..}
e) {..}
catch (IOException
IOException e) {..}
e) {..}
}
3
import
java
import
java
iio
public class URLReader
public static void main
String
[]
args
))
Exception
URL y =
URL y = new
URL("http://
www
yahoo
com
BufferedReader in
==
new InputStreamReader
(y.
openStream
String inputLine
while
(((
inputLine
= in.
readLine
()) !=
null
System.
out
println
inputLine
));
in.
close
}}
}}}
try
try {
URL
URL
yahoo
==
new
URL("http://
www
yahoo
com
yahoo
openConnection
}
}}
catch
MalformedURLException
catch
IOException
648092962.005.png 648092962.006.png 648092962.007.png 648092962.008.png 648092962.009.png
URL Î getContent()
czc
zz
public final Object getContent
()
throws IOException
(()
throws IOException
zc
zc
zcc
zc
zzczc
c
throws IOException
URLConnection
cc
cccc
c
cc
c
c
zzcc
ccz
c
c
4
public final Object getContent
public final Object getContent()
648092962.010.png 648092962.011.png 648092962.012.png 648092962.013.png 648092962.014.png
URLConnection - czytanie
import java
java.net.*;
.net.*;
.*;
public class URLConnectionReader
java.io
io.*;
.*;
public class URLConnectionReader {
public static void main
public static void main(String
String[]
[] args
args)
) throws
throws
Exception {
URL y =
/");
URLConnection yc = y.openConnection();
BufferedReader in
new URL("http://
URL("http://www
www.yahoo
yahoo.com
com/");
/");
BufferedReader in =
new BufferedReader(
new InputStreamReader
= new BufferedReader
new BufferedReader
new InputStreamReader(yc.getInputStream()));
));
String inputLine;
;
while ((
((inputLine
inputLine = in.
= in.readLine
readLine()) !=
()) != null
null)
))
System.out
out.println
println(inputLine
inputLine);
));
in.close
close();
();
}
}
Adresy internetowe
z
czc
zcc
ccz
cz z
z
public static InetAddress getByName(String host
String host)
public static InetAddress[]
[] getAllByName(String host
String host)
public static InetAddress getLocalHost()
(()
5
import
java
import
import java
java
iio
public class URLConnectionReader
public static void main
String
[]
args
))
Exception
URL y =
URL y = new
new
URL("http://
www
yahoo
com
BufferedReader in
==
new InputStreamReader
String inputLine
;;
while
(((
inputLine
= in.
readLine
()) !=
null
)
System.
out
println
inputLine
);
in.
close
public static InetAddress
String host
public static InetAddress
[[]
String host
public static InetAddress
()
648092962.015.png 648092962.016.png 648092962.017.png 648092962.018.png 648092962.019.png
Zgłoś jeśli naruszono regulamin