getProtocol
URL ning protokolini qaytaradi
getAuthority
URL ning authority qismini qaytaradi.
getHost
URL ning hostini qaytaradi.
getPort
URL ning portini qaytaradi. Agar port mavjud bo’lmasa -1 qaytadi.
getPath
URL ning path(yo’l) qismini qaytaradi.
getQuery
URL ning so’rov qismini qaytaradi.
getFile
URL ning fayl qismini va so’rov qismini qaytaradi.
getRef
URL ning reference qismini qaytaradi.
JAVA: SWING
import java.net.*;
import java.io.*;
public class
ParseURL {
public static void
main(String[] args)
throws
Exception {
URL aURL = new URL("
http://java.sun.com:80/docs/books/tutorial"
+ "/index.html?name=networking#DOWNLOADING
");
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
}
}
JAVA: SWING
protocol = http
authority = java.sun.com:80
host = java.sun.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING
JAVA: SWING
import java.net.*;
import java.io.*;
public class
URLReader {
public static void
main(String[] args)
throws
Exception {
URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Natija: Ekranda yahoo serveridan qaytgan html dokumtn.
JAVA: SWING
URL ga ulanish uchun URLConnection klasidan foydalanish kerak. URL klasining
openConnection metodini URLConnection niga obyekt qaytaradi.
try {
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yahooConnection = yahoo.openConnection();
yahooConnection.connect();
} catch (MalformedURLException e) {
…
} catch (IOException e) {
. . .
}
import java.io.*;
import java.net.*;
public class
Reverse {
public static void
main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: java Reverse " +
"http://" +
" string_to_reverse");
System.exit(1);
}
String stringToReverse = URLEncoder.encode(args[1], "UTF-8");
URL url = new URL(args[0]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
JAVA: SWING
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write("string=" + stringToReverse);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
}
}
Do'stlaringiz bilan baham: |