Вы находитесь на странице: 1из 4

Data Communication & Networking I week 3 Practical Lecture

URL
It uses to analysis the URL on java applications such as the domain name, port
number, path, and etc. in order to deal with specific part of the URL.
URL class  Class URL represents a Uniform Resource Locator, a pointer to a
"resource" on the World Wide Web. A resource can be something as simple as a file
or a directory, or it can be a reference to a more complicated object, such as a query
to a database or to a search engine.
The used functions when you create object are as follow:
1. getFile()  The file name of this URL, or an empty string if one does not
exist. The returned file portion will be the same as getPath(), plus the
concatenation of the value of getQuery(), if any. If there is no query portion,
this method and getPath() will return identical results.
2. getProtocol()  Returns the protocol name of this URL.
3. getHost()  Returns the host name of the URL (domain name).
4. getPort()  Returns the port number, or -1 if the port is not set of the URL.
5. getAuthority()  Returns the hostname of the URL plus the port number if
exists.
6. getDefaultPort()  Returns the default port number of the protocol
associated with this URL. If the URL scheme or the URLStreamHandler for
the URL do not define a default port number, then -1 is returned.
7. getQuery()  Returns the query part of this URL, or null if one does not
exist.
8. getPath()  Returns the path part of this URL, or an empty string if one does
not exist.
URLConnection class  The abstract class URLConnection is the superclass of all
classes that represent a communications link between the application and a URL.
Instances of this class can be used both to read from and to write to the resource
referenced by the URL. Returns an URLConnection instance that represents a
connection to the remote object referred to by the URL.
The used functions when you create object are as follow:
1. getHeaderField(i)  Returns the value for the nth header field. It returns null
if there are less than n+1 fields. This method can be used in conjunction with

1|P ag e By: Ghasaq Bahaa


Data Communication & Networking I week 3 Practical Lecture

the getHeaderFieldKey method to iterate through all the headers in the


message.
2. getHeaderFieldKey(i)  Returns the key for the nth header field. It returns
null if there are fewer than n+1 fields.
3. getExpiration()Returns the value of the expires header field. The
expiration date of the resource that this URL references, or 0 if not known.
The value is the number of milliseconds.
4. getLastModified()  Returns the value of the last-modified header field.
The result is the number of milliseconds or 0 if not known.
5. getPermission()  Returns a permission object representing the permission
necessary to make the connection represented by this object. This method
returns null if no permission is required to make the connection. By default,
this method returns java.security.AllPermission. The permission returned
may dependent upon the state of the connection. For example, the permission
before connecting may be different from that after connecting. For example,
an HTTP server, say foo.com, and may redirect the connection to a different
host, say bar.com. Before connecting the permission returned by the
connection will represent the permission needed to connect to foo.com, while
the permission returned after connecting will be to bar.com. Permissions are
generally used for two purposes: to protect caches of objects obtained
through URLConnections, and to check the right of a recipient to learn about
a particular URL. In the first case, the permission should be obtained after
the object has been obtained. For example, in an HTTP connection, this will
represent the permission to connect to the host from which the data was
ultimately fetched. In the second case, the permission should be obtained and
tested before connecting.

2|P ag e By: Ghasaq Bahaa


Data Communication & Networking I week 3 Practical Lecture

Code:

Notes:
1. For loop does not have a condition to stop it, which means we need to put
a condition to do so. In our case, we put the value of the header field as a
condition; when it is null to stop the infinite loop.
2. Ports numbers 0-1024 are reserved for privileged services and designated
as well-known ports which defined the ports as an endpoint to a logical
connection and the way a client program specifies a specific server
program on a computer in a network:

3|P ag e By: Ghasaq Bahaa


Data Communication & Networking I week 3 Practical Lecture

Port number Used in


7 ECHO
20 FTP data
21 FTP control
22 SSH
23 Telnet
25 Simple Mail Transfer Protocol (SMTP)
53 Domain Name System (DNS)
80 HTTP
115 Simple File Transfer Protocol (SFTP)
443 HTTPS

 Well-known ports 0-1023


 Registered ports 1024-49151
 Dynamic ports 49152-65535
3. HTTP stands for “Hypertext Transfer Protocol”. Almost everything you
use in your browser is transmitted to your computer over HTTP. HTTP
headers are the core part of HTTP requests and responses. They carry
information about the client browser, the requested page, the server, and
more. In the request, the first line has 3 things:
 Method …. (GET, POST, or HEAD)
 Path …. Part of URL
 Protocol …. HTTP along with its version
The response has the status code along with number of parameters to give
details about the connection, server, expiry date, and etc. the following is
examples about status code:
I. 200 ok  send in response to a successful request.
II. 404 Not Found  the requested page or file not found, it sends by
the server.
III. 401 Unauthorized  password protection.
IV. 403 Forbidden  you are not allowed to access the page.
V. 500 Internal Server Error  web script crashes.
References:
https://docs.oracle.com/javase/7/docs/api/java/net/URL.html
https://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html
https://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039

4|P ag e By: Ghasaq Bahaa

Вам также может понравиться