Lecture 9: Network programming

Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. Sample structure of a URL. The resource name part may contain: host name, file | Lecture 9: Network programming Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. Sample structure of a URL. The resource name part may contain: host name, file name, port number(optional) and reference (optional) you can create a URL object in Java from an absolute URL or a relative URL. The URL class provides several methods implemented on URL objects. You can get the protocol, host name, port number, and filename from a URL. Protocol Identifier Resource Name Example code import .*; import .*; public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL("" + "tutorial/"); ("protocol = " + ()); ("host = " + ()); ("filename = " + ()); ("port = " + ()); ("ref = " + ()); } } Output of the above code: protocol = http host = filename = /docs/books/tutorial/ port = 80 ref = DOWNLOADING Connecting with a URL (1) openStream(): returns a object, from which you can read easily as reading from an input stream. It may throw an IOException Example code import .*; import .*; public class ReadURL { public static void main(String[] args) throws Exception { URL osu = new URL(""); BufferedReader in = new BufferedReader( new InputStreamReader(())); String inputLine; while ((inputLine = ()) != null) (inputLine); (); } } This prints out the source code for the webpage Connecting with a URL (2) openConnection(): Returns a URLConnection object that represents a connection to the remote object referred to by the URL. It may throws an IOException try { URL osu = new . | Lecture 9: Network programming Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. Sample structure of a URL. The resource name part may contain: host name, file name, port number(optional) and reference (optional) you can create a URL object in Java from an absolute URL or a relative URL. The URL class provides several methods implemented on URL objects. You can get the protocol, host name, port number, and filename from a URL. Protocol Identifier Resource Name Example code import .*; import .*; public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL("" + "tutorial/"); ("protocol = " + ()); ("host = " + ()); ("filename = " + ()); ("port = " + ()); .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.