[go: up one dir, main page]

0% found this document useful (0 votes)
11 views109 pages

Networking

The document provides an overview of network programming in Java, covering topics such as connecting to servers, socket communication, and the distinction between connection-oriented and connectionless services. It explains the use of well-known ports, the role of sockets in establishing communication between nodes, and includes examples of client and server implementations. Additionally, it discusses the handling of host names and IP addresses, as well as the use of the InetAddress class for network address resolution.

Uploaded by

lujein al-shehri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views109 pages

Networking

The document provides an overview of network programming in Java, covering topics such as connecting to servers, socket communication, and the distinction between connection-oriented and connectionless services. It explains the use of well-known ports, the role of sockets in establishing communication between nodes, and includes examples of client and server implementations. Additionally, it discusses the handling of host names and IP addresses, as well as the use of the InetAddress class for network address resolution.

Uploaded by

lujein al-shehri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 109

1

Networking

C o r e j a v a v o l u m e 2: ch a pt er 4
Objectives 2

• Connecting to a server

• Implementing servers

• Socket communication

• URL network communication


Network Programming and Java 3

• Java abstracts all of the details of the underlying


network, and how the operating system interacts
with it. All of this is hidden and encapsulated in
the java.net package

• A computer on a network has an address

• This address is used to uniquely identify this


computer (also known as a host) on the network.

• The address system can be IPv4 or IPv6


Ports 4

• Each host on the network has a set of ports

• The address specifies the host, and the port specifies


the application on that host

• The port identifiers are unsigned 16-bit integers. So, ports


range from 1 to 216-1 = 65535.

• Port 0 is a reserved port in TCP/IP networking, meaning


that it should not be used in TCP or UDP messages

• These ports allow multiple applications to use the


same network address to communicate over the
network
Well-Known Ports 5

• Port numbers below 1024 are well-known ports

• These port numbers are assigned to various applications; for


example, HTTP server is always connected to port 80, an FTP
server is always connected to port 21, etc…

• These well-known ports are assigned to servers. When a client


makes a connection to a server, the client uses another port
(above 1024) to connect to a server’s well-known port

• There is no technical reason servers must conform to these


standards – it is just a convention so that clients will always know
where the web server is, where the FTP server is, and so forth
6

21 FTP server

23
192.168.10.253
Telnet server

“the network” 80 HTTP server

3477 HTTP client


Sockets 7

• Socket programming is a way of connecting two


nodes on a network to communicate with each
other

• A socket is an abstraction of an endpoint of


a two-way communications link

• One socket (node) listens on a particular


port at a specific host, while
other socket try to form a connection
Sockets 8

• When a socket is created, a port (could be


random) on the host (client) is chosen and a
“connection” is created between the client
using this port and the specified remote
address using the remote port.
Connection Services 9

• There are two primary types of connection


services provided by a computer network

1. A connection-oriented service

2. A connectionless service
Connection-Oriented Service 10

• The connection-oriented service acts like


a pipe

• The sender pushes object bits into the pipe


and then come out of the receiver in the
same order as they were pushed in

• This pipe is connected to one port on the


sender and one port on the receiver
Connection-Oriented Service 11

• The connection-oriented service is implemented


in Java using stream sockets

• Once a stream socket is open on both ends (the


server and the client), and the sockets connect to
each other, there is a pipe connecting them,
providing a reliable byte stream

• The most popular protocol that implements a


stream, or connection-oriented, service is the
Transmission Control Protocol (TCP)
Connectionless Service 12

• The connectionless service acts like the postal


system. The primary characteristic of this type of
service is that one side sends messages to the
other side

• This connection is unreliable. All sorts of things can


happen to the messages during transport in the
network (although most of the time, they get there just
fine)

• One side creates a message and sends it to the other


side
Connectionless Service 13

• The connectionless service is implemented in


Java using datagram sockets

• The connectionless sockets do not establish


a connection before they communicate

• The most popular protocol that implements


the connectionless service is the User
Datagram Protocol (UDP)
Create client program that connect to a server 14

• We will make a client program that makes


a socket connection to the atomic clock
in NIST (National Institute of Standards and
Technology) and prints the time that the
server sends. (Of course, the reported time is
not completely accurate due to network
delays.)
Example1: SocketTest 15
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
/**
* This program makes a socket connection to the atomic clock in Boulder,
Colorado, and prints
* the time that the server sends.
*/
public class SocketTest
{
public static void main(String[] args) throws IOException
{
try (var s = new Socket("time-a.nist.gov", 13);
var in = new Scanner(s.getInputStream(), StandardCharsets.UTF_8))
{
while (in.hasNextLine())
{
String line = in.nextLine();
System.out.println(line);
}
}
}
}
Example1: SocketTest output 16
C:\Users\Asus\Desktop\IT\CPIT305\Slides Hind\Network>java SocketTest

59640 22-03-02 17:09:42 62 0 0 874.7 UTC(NIST) *

C:\Users\Asus\Desktop\IT\CPIT305\Slides Hind\Network>java SocketTest

59640 22-03-02 17:09:44 62 0 0 570.4 UTC(NIST) *

C:\Users\Asus\Desktop\IT\CPIT305\Slides Hind\Network>java SocketTest

59640 22-03-02 17:09:55 62 0 0 407.9 UTC(NIST) *


Example1: SocketTest explanation 17

• You have connected to the “time of day”


service that most machines constantly run

• By convention, the “time of day” service is


always attached to “port” number 13
Example1: SocketTest explanation 18

• The following line will open a socket:

var s = new Socket("time-a.nist.gov", 13);

• We pass the remote address and the port number to


the socket constructor

• If the IP address of the host could not be determined,


an UnknownHostException is thrown.

• If an I/O error occurs when creating the socket, an


IOException occurs. Since UnknownHostException is a
subclass of IOException, we just throws the superclass
Example1: SocketTest explanation 19

• Once the socket is open, the getInputStream method in


java.net.Socket returns an InputStream instance that
you can use just like any other stream

• The java.net package essentially gives you the same


programming interface you would use to work with a file

• Closing the returned InputStream will close the


associated socket.

• Closing this socket will also close the socket's


InputStream and OutputStream.
Example2: SocketTest2 20

• In the following slide, we show the same


example with catching of exception instead of
throws
Example2: SocketTest2 21
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
public class SocketTest2
{
public static void main(String[] args)
{
try
{
var s = new Socket("time-a.nist.gov", 13);
try
{
var in = new Scanner(s.getInputStream(), StandardCharsets.UTF_8);

while (in.hasNextLine())
{
String line = in.nextLine();
System.out.println(line);
}
}//inner try
finally
{
s.close();
}
}//outer try
catch(IOException ex)
{
System.out.println("We have the following exception: "+ex);
}
}
}
Example3: SocketTest3 22

• Example3 is the same client in Example1,


however we print some information about the
IP addresses and the ports
Example3: SocketTest3 23
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
public class SocketTest3
{
public static void main(String[] args) throws IOException
{
try (var s = new Socket("time-a.nist.gov", 13);
var in = new Scanner(s.getInputStream(), StandardCharsets.UTF_8);)
{

System.out.println("The address to which the socket is connected:


"+s.getInetAddress());
System.out.println("The remote port number to which this socket is connected:
"+s.getPort());
System.out.println("The local address to which the socket is bound:
"+s.getLocalAddress());
System.out.println("The local port number to which this socket is bound:
"+s.getLocalPort());
while (in.hasNextLine())
{
String line = in.nextLine();
System.out.println(line);
}
}
}
}
Example3: SocketTest3 output 24
The address to which the socket is connected: time-a.nist.gov/129.6.15.28

The remote port number to which this socket is connected: 13

The local address to which the socket is bound: /192.168.1.4

The local port number to which this socket is bound: 62425

58922 20-03-14 16:49:33 50 0 0 173.3 UTC(NIST) *


Host Names and IP Addresses 25

• Notice that a name is provided to the socket constructor,


not an IP address

• This name is a host name. Java will resolve this name


into an IP address and then connect to the host using
that IP address

• Usually, you will not have to work directly with IP


addresses. However, it is possible to directly lookup the IP
address of a host

• You can use the InetAddress class if you need to convert


between host names and Internet addresses
Host Names and IP Addresses 26

• This is accomplished by the InetAddress class’s static


method, getByName()

• For example,
InetAddress address =
InetAddress.getByName("www.kau.edu.sa");
will return an InetAddress object that encapsulates the
sequence of four bytes

• The following will print the IP address

System.out.println("address: " + address.getHostAddress());

• As: 192.162.72.233
Host Names and IP Addresses 27

• Some host names with a lot of traffic correspond


to multiple Internet addresses, to facilitate load
balancing

• One of them is picked at random when the host is


accessed. You can get all the IP addresses of a
host with the getAllByName method

• InetAddress[] addresses =
InetAddress.getAllByName("www.google.com");
The Loopback Address and localhost 28

• The local loopback address may be used to


run a network service on a host without
requiring a physical network interface, or
without making the service accessible from
the networks the computer may be connected
to

• The loopback address is 127.0.0.1


The Loopback Address and localhost 29

• If you simply ask for the address of localhost,


you always get the local loopback address
127.0.0.1
• InetAddress loop =
InetAddress.getByName("localhost");
System.out.println("localhost: " +
loop.getHostAddress());

• Will print 127.0.0.1


The Loopback Address and localhost 30

• As sometimes it is necessary to get information about


the machine the program is running on. The machine
where you are working is called local host

• In order to get the actual IP address of the local host,


call the static method getLocalHost(). This will
return the actual IP address of the host on the network
InetAddress localHostAddress = InetAddress.getLocalHost();
System.out.println("Actual local host:
"+localHostAddress.getHostAddress());

Will print 192.168.1.4


Server sockets 31

• In Example1, we have implemented a basic


network client that receives data from the
Internet

• Now we will program a simple server that can


send information to clients

• A server program, when started, must be


waiting (listening) for a client to attach to
its port
Server sockets 32

• For our example program, we chose port


number 8189, which is not used by any of the
standard services

• The ServerSocket class establishes a socket

• To establish a server that monitors 8189:


var server = new ServerSocket(8189);
Server sockets 33

• The command:
Socket incoming = server.accept();

tells the server to wait indefinitely until a client


connects to that port

• Once someone connects to this port (8189) by


sending the correct request over the network,
this method returns a Socket object that
represents the connection that was made
Server sockets 34

• You can use this client object to get input and


output streams, as is shown in the following code:
InputStream inStream = incoming.getInputStream();

OutputStream outStream = incoming.getOutputStream();

• Everything that the server sends to the server


output stream (outStream) becomes the input of
the client program, and all the output from the
client program ends up in the server input stream
(inStream)
Server sockets 35
• We therefore deal with these streams using Scanner
and PrintWriter
var in = new Scanner(inStream, StandardCharsets.UTF_8);

var out = new PrintWriter(new OutputStreamWriter(outStream,


StandardCharsets.UTF_8), true /* autoFlush */); on java 8

• Or if you work on java 11 or later, PrintWriter can be as:


var out = new PrintWriter(outstream, true /* autoFlush */,
StandardCharsets.UTF_8); on java 11

• This is because PrintWriter constructor in java 8 cannot


change the charset
Server sockets 36
• autoFlush - A boolean; if true, the println, printf,
or format methods will flush the output buffer

• Actually, PrintWritter has a buffer. This class will


not flush the buffer except in two cases:
• when you close the stream or,

• when you call print, println, format if you set the


autoFlush Boolean to true

• If you didn’t set autoFlush to true, the output will


not reach to the client until you close the stream.
Example4: EchoServer 37

• In this simple server (EchoServer), we just read the


client’s input, a line at a time, and echo it until the client
enter BYE. This demonstrates that the server receives the
input from the client

• We will communicate with the server using telnet

• Telnet is an application protocol used on the Internet or


local area network to provide a two-way interactive text
communication facility.

• Then we will write a client (EchoClient) that


communicate with the server
Example4: EchoServer page 1 38
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;

public class EchoServer


{
public static void main(String[] args)
{
try (var s = new ServerSocket(8189);
Socket incoming = s.accept();
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
var in = new Scanner(inStream, StandardCharsets.UTF_8);
var out = new PrintWriter(outStream, true /* autoFlush */,
StandardCharsets.UTF_8);
){
Example4: EchoServer page 2 39
out.println("Hello! Enter BYE to exit.");
// echo client input
var done = false;
while (!done )
{
String line = in.nextLine();
out.println("Echo: " + line);
if (line.trim().equals("BYE"))
done = true;
}
}//end of try-with
catch(IOException e)
{
System.out.println("Exception: " + e);
}
}
}
Example4: EchoServer output 40

• When you run the server (I am using CMD)


you will see no thing, only the server is
running and waiting for any connection
Example4: EchoServer output

41
Example4: EchoServer output using telnet

42
Example4: EchoServer output using telnet 43
Example5: EchoClient 44
public class EchoClient
{ public static void main(String[] args)
{
try(var client = new Socket("127.0.0.1", 8189);
var in = new Scanner(client.getInputStream(),
StandardCharsets.UTF_8);
PrintWriter out = new PrintWriter(client.getOutputStream(),
true, StandardCharsets.UTF_8);
){
String str;
str = in.nextLine();
System.out.println("Server says: " + str);
out.println("This is the client");
str = in.nextLine();
System.out.println("Server says: " + str);
out.println("BYE");
str = in.nextLine();
System.out.println("Server says: " + str);

}//end of try-with
catch(IOException e)
{
System.out.println("Exception in our client: " + e);
} } }
Example5: EchoClient output 45

• If you run the client while there is no server process running


currently, you will get the runtime exception
java.net.ConnectException
Servers and Multiple Clients 46

• Normally, servers handle multiple clients at


once. If a server only allowed one client to
connect at any given time, any client can
monopolize the service by remaining
connected to the server for a long time

• This situation can be improved upon by


allowing multiple clients to connect at once
using threads
Servers and Multiple Clients 47

• Every time we know the server program has


established a new socket connection—that is,
every time the call to accept() returns a
socket—we will launch a new thread to
take care of the connection between the
server and that client

• The main thread of the server program


will just go back and wait for the next
connection from a client
Servers and Multiple Clients 48

• For this to happen, the main loop of the server


should look like this:

• while (true)
{

Socket incoming = server.accept();

var r = new ThreadedEchoHandler(incoming);

var t = new Thread(r);

t.start();

}
Servers and Multiple Clients 49

• The ThreadedEchoHandler class implements


Runnable and contains the communication
loop with the client in its run method

• When each connection starts a new thread,


multiple clients can connect to the server
at the same time
Servers and Multiple Clients 50

• You can easily check this out:


1. Compile and run the server program

2. Open several telnet windows

3. Switch between windows and type commands.


Note that you can communicate through all of
them simultaneously

4. When you are done, switch to the window from


which you launched the server program and press
Ctrl+C to kill it
Example6: ThreadedServer page 1 51
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
public class ThreadedServer
{
public static void main(String[] args )
{
try (var s = new ServerSocket(8189))
{
int i = 1;

while (true)
{
Socket incoming = s.accept();
System.out.println("Spawning client: " + i);
Runnable r = new ThreadedEchoHandler(incoming);
var t = new Thread(r);
t.start();
i++;
}
}
catch (IOException e)
{
e.printStackTrace();
} } }
Example6: ThreadedServer page 2 52
class ThreadedEchoHandler implements Runnable
{
private Socket incoming;
public ThreadedEchoHandler(Socket incomingSocket)
{
incoming = incomingSocket;
}
public void run()
{
try (InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
var in = new Scanner(inStream, StandardCharsets.UTF_8);
var out = new PrintWriter(outStream, true /* autoFlush */,
StandardCharsets.UTF_8);
){
out.println( "Hello! Enter BYE to exit." );
var done = false;
while (!done)
{
String line = in.nextLine();
out.println("Echo: " + line);
if (line.trim().equals("BYE"))
done = true;
} }
catch (IOException e)
{
e.printStackTrace();
} } }
Example6: ThreadedServer output
Server is running but there are no clients yet 53
Example6: ThreadedServer output 54

• Now we will make three clients that will


connect to the server. The clients will be
implemented using telnet command:
>telnet localhost 8189

• Or instead of telnet client, you can use the


program of Example5 (EchoClient) as client
that connects to the server
Example6: ThreadedServer output
First client connected 55
Example6: ThreadedServer output
Second client also connected 56
Example6: ThreadedServer output
Third client also connected 57
Example6: ThreadedServer output
All the 3 clients talked to the server and closed 58
their connection. However the server is still
opening and waiting for new client. Press CTR+c
to kill the server
Other Types of Network Streams 59

• So far, we have connected Scanner and


PrintWriter to our socket’s input and output
streams. This was done as we wanted to
receive and send text from the streams

• Suppose we wanted to exchange typed data


over a network socket. This is very easy;
simply attach a DataInputStream object and a
DataOutputStream object to the socket
Typed Data Network Streams 60

• For example, for a client program, we first create a


Socket object, obtain the input and output streams
associated with that socket, and then attach
DataInputStream and DataOutputStream objects…
• Socket client = new Socket(“127.0.0.1”,8189);

• var in = new DataInputStream(client.getInputStream());

• var out = new DataOutputStream(client.getOutputStream());

• double Data1 = in.readDouble();

• double Data2 = 2.5;

• out.writeDouble(Data2);
Object Network Streams 61

• We can follow a similar approach if we want to


transfer objects over the network (remember that the
objects must be Serializable)…
• Socket client = new Socket(“127.0.0.1”,8189);

• var in = new ObjectInputStream(client.getInputStream());

• var out = new


ObjectOutputStream(client.getOutputStream());

• Employee Obj1 = (Employee)in.readObject();

• Employee Obj2 = new Employee (“Sara”, “75000”, 2010);

• out.writeObject(Obj2);
Socket Timeouts of reading from Socket 62

• Reading from a socket blocks until data


are available

• If the host is unreachable, your application


waits for a long time and you are at the mercy
of the underlying operating system to
eventually time out
Socket Timeouts of reading from Socket 63

• Java support a timeout value for just such an


occurrences.
var s = new Socket(. . .);

s.setSoTimeout(10000); // time out after 10 seconds

• If the timeout value has been set for a socket, all


subsequent read operations throw a
SocketTimeoutException when the timeout has
been reached before the operation has
completed its work
Socket Timeouts of reading from Socket 64
You can catch that exception and react to the timeout:

try (var s = new Socket(. . .) )

{ s.setSoTimeout(10000); // time out after 10 seconds

InputStream in = s.getInputStream();

. . . // read from server, here we will have the timeout exception

catch (SocketTimeoutException e) // SocketTimeoutException is one of the


subclasses of IOException

{ // react to timeout

} catch (IOException e) // because of Socket(. . .) and


s.setSoTimeout(10000)

{… }
Socket Timeouts of connection establishment 65

• There is one additional timeout issue that you


need to address. The constructor:
Socket(String host, int port)

• can block indefinitely until an initial


connection to the host is established
Socket Timeouts of connection establishment 66

• You can overcome this problem by first constructing an


unconnected socket and then connecting it with a
timeout
var s = new Socket();

s.connect(new InetSocketAddress(host, port), timeout);

Ex. s.connect(new InetSocketAddress(“127.0.0.1”,8189),


5000);

The timeout is measure in milliseconds

• SocketTimeoutException (checked exception) is


thrown if timeout expires before connecting
Getting web data 67

• Socket-level programming is quite powerful as your


application directly controls what is sent and directly
receives what is received

• This is not very convenient to transfer network data


using a well-known protocol such as HTTP as your
application would need to implement the HTTP protocol
(i.e. generate headers and correctly format the HTTP
packets)

• To access web servers in a Java program, you will want


to work at a higher level than socket connections
URLs and URIs 68

• The URL and URLConnection classes


encapsulate much of the complexity of
retrieving information from a remote site

• You can construct a URL object from a string:


var url = new URL(urlString);

urlString is the host name such as:

var url = new URL(“http://horstmann.com”);


URLs and URIs 69

• If you simply want to fetch the contents of the


resource, use the openStream method of the
URL class. This method yields an
InputStream object. Use it in the usual way
— for example, to construct a Scanner:

• InputStream inStream = url.openStream();

• var in = new Scanner(inStream,


StandardCharsets.UTF_8);
URLs and URIs 70

• The java.net package makes a useful distinction


between URLs (uniform resource locators) and URIs
(uniform resource identifiers)

• A URI is a purely syntactical construct that


contains the various parts of the string specifying a
web resource

• In the Java library, the URI class has no methods for


accessing the resource that the identifier specifies
—its sole purpose is parsing the identifier
URLs and URIs 71

• A URL is a special kind of URI, namely, one


with sufficient information to locate a resource

• The URL class can open a stream to the


resource.

• For that reason, the URL class only works


with schemes that the Java library knows how
to handle, such as http:, https:, and ftp:
Relative URLs 72
• Relative URLs are URL objects constructed using an
absolute URL as a starting point

• For example, suppose your program needs to create URL


objects for these two network resources:
• http://www.kau.edu.sa/page1

• http://www.kau.edu.sa/page2

• This could be done by creating a common URL and then


relative URLs for differences:
• var baseURL = new URL(“http://www.kau.edu.sa”);

• var file1URL = new URL(baseURL, “page1”);

• var file2URL = new URL(baseURL, “page2”);


URLs and Exceptions 73

• Creating a URL object can throw a


MalformedURLException (checked exception) if the
constructor arguments are null or refer to an unknown host

• So, such construction must be placed inside a try/catch


block
try { var myURL = new URL ( . . . ); }

catch (MalformedURLException exp) { }

• Or

try { var myURL = new URL ( . . . ); }

catch (IOException exp) { }


Using a URLConnection to Retrieve Information 74

• If you want communication with a web resource,


you should use the URLConnection class

• URLConnection class gives you much more control


than the basic URL class

• When working with a URLConnection object, you


must carefully schedule your steps:

1. Call the openConnection method of the URL


class to obtain the URLConnection object:
URLConnection connection = url.openConnection();
Using a URLConnection to Retrieve Information 75

2. Set any request properties, using the


methods:
o setDoInput

o setDoOutput

o setConnectTimeout
Using a URLConnection to Retrieve Information 76

3. Connect to the remote resource by calling


the connect method.
connection.connect();

Besides making a socket connection to the server,


this method also queries the server for header
information
Using a URLConnection to Retrieve Information 77

4. Finally, you can access the resource data.


Use the getInputStream method to obtain an
input stream for reading the information (This is
the same input stream that the
openStream method of the URL class returns.)
URLConnection methods 78

• You can read the header information using methods such


as:
• connection.getContentType();

• connection.getContentLength();

• connection.getContentEncoding();

• connection.getLastModified(); The result is the number of


milliseconds since January 1, 1970 GMT.

Use new Date(connection.getLastModified()) to know exact date

• Also, you can read the content of the website:


• connection.getInputStream()
Example7: URLConnectionTest page 1 79
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
public class URLConnectionTest
{ public static void main(String[] args)
{
try
{ String urlName = "https://horstmann.com";
var url = new URL(urlName);
URLConnection connection = url.openConnection();
connection.connect();
// print header functions
System.out.println("----------");
System.out.println("getContentType: " + connection.getContentType());
System.out.println("getContentLength: " + connection.getContentLength());
System.out.println("getContentEncoding: " +
connection.getContentEncoding());
System.out.println("getLastModifed: " + new
Date(connection.getLastModified()));
System.out.println("----------");
Example7: URLConnectionTest page 2 80
String encoding = connection.getContentEncoding();
if (encoding == null) encoding = "UTF-8";
try (var in = new Scanner(connection.getInputStream(), encoding))
{ // print first 10 lines of contents
for (int n = 1; in.hasNextLine() && n <= 10; n++)
System.out.println(in.nextLine());
if (in.hasNextLine()) System.out.println(". . .");
}
}
catch (IOException e)
{
e.printStackTrace();
} } }
Example7: URLConnectionTest output 81
getContentType: text/html; charset=utf-8

getContentLength: 6173

getContentEncoding: null

getLastModifed: Sun Feb 27 19:12:54 AST 2022


----------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-


strict.dtd">

<html xmlns='http://www.w3.org/1999/xhtml'>

<head>

<title>Cay Horstmann's Home Page</title>

<link href='styles.css' rel='stylesheet' type='text/css'/>

<meta http-equiv='content-type' content='text/html; charset=UTF-8'/>

</head>

<h1>Welcome to Cay Horstmann?s Home Page!<img class='sideimage' alt='Photo of Cay Horstmann'


src='images/cay-rowing.gif' loading='lazy'/></h1>

<p><a href='mailto:cay@horstmann.com'>cay@horstmann.com</a> | <a


href='http://horstmann.com/caypubkey.txt'>PGP Key</a></p>
Posting form data 82

• In the preceding section, you saw how to read data from


a web server. Now we will show you how your programs
can send data back to a web server

• To send information from a web browser to the web


server, a user fills out a form

• When the user clicks the Submit button, the text in the
text fields and the settings of any checkboxes, radio
buttons, and other input elements are sent back to the
web server. The web server invokes a program that
processes the user input
Posting form data 83

• Many technologies enable web servers to invoke


programs. Among the best-known ones are
JavaServer Faces, Microsoft Active Server Pages
(ASP), and Common Gateway Interface (CGI)
scripts

• The server-side program processes the form


data and produces another HTML page
that the web server sends back to the
browser
Posting form data 84

• We do not discuss the implementation of


server-side programs in this book. Our interest
is merely in writing client programs that
interact with existing server-side programs
Posting form data 85

• HTTP (Hypertext Transfer Protocol), is the


underlying format that is used to structure request
and responses for effective communication
between a client and a server.

• The message that is sent by a client to a


server is what is known as an HTTP request

• HTTP request methods include:


• GET

• POST
GET command 86

• In the GET command, you simply attach


query parameters to the end of the URL. The
URL has the form
• http://host/path?query

• Each parameter has the form name=value.


Parameters are separated by & characters
Posting form data 87

• Parameter values are encoded using the URL encoding


scheme, following these rules:

• Leave the characters A through Z, a through z, 0 through 9,


and . - ~ _ unchanged

• Replace all spaces with + characters

• Encode all other characters into UTF-8 and encode each


byte by a %, followed by a two-digit hexadecimal
number

• For example, to transmit San Francisco, CA, you use


San+Francisco%2c+CA, as the hexadecimal number 2c is the
UTF-8 code of the ‘,' character
Posting form data 88

• We can use the following command to make


this encoding:
• String str=URLEncoder.encode(“San Francisco,
CA”, StandardCharsets.UTF_8)

• str will be: “San+Francisco%2c+CA”


Posting form data 89

• Another example: the web server


https://www.google.com has page “search”
• Hence: the URL is https://www.google.com/search

• This page has many parameters, and we will use the


following:

• q=the string we search for


the words should be separated by +

• as_eq=don’t include these words in results

• as_filetype=extension

• num=Set the number of results per page to display


Posting form data 90

• Assume the following:


• we want to search for the words: kau university

• And we want to exclude the words: FCIT

• And we want the results to be only of pdf files

• And we the number of results per page of the search process


to be 2

• The URL will be written as:


• https://www.google.com/search?
q=kau+university&as_eq=FCIT&as_filetype=pdf&num=2

• Notice that the parameter list starts with a ‘?’ and the
individual parameters are separated with a ‘&’
Example8: HTTPGet page 1 91
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;
public class HTTPGet
{ public static void main(String[] args)
{
try
{ String urlName =
"https://www.google.com/search?q=kau+university&as_eq=FCIT&as_filetype=pdf&num=2
";
var url = new URL(urlName);
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0;
Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74
Safari/537.36");
connection.connect();
try (var in = new Scanner(connection.getInputStream(),
StandardCharsets.UTF_8))
{ while(in.hasNextLine())
System.out.println(in.nextLine());
}
}
catch (IOException e)
{
e.printStackTrace();
} } }
Example8: HTTPGet explanation 92

• HTTP headers are the core part of the HTTP requests and
responses, and they carry information about the client,
the requested page, and the server

• HTTP headers are fields that are represented by name


and value pairs

• One of these headers is user-agent

• user-agent is a string that lets servers identify the


browser, its version and some system information

• Your browser sends the user-agent to every


website you connect to
Example8: HTTPGet explanation 93

• The output of Example8 is html content that


carry the result of the google search process.
This content can be presented using any html
viewer
Example8: HTTPGet output 94
Example9: HTTPGet1 95

• In Java 11, a new HttpClient is introduced in package


java.net.http.*

• HTTPClient is more powerful and flexible

• We will use three classes:


• HttpClient: to build client

• HttpRequest: to build HTTP request

• HttpResponse: to receive HTTP response

• In the following example we will repeat the same google


search we applied in Example8, with different user-
agent
Example9: HTTPGet1 96
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.nio.file.*;
import java.net.http.*;
import java.util.*;
public class HTTPGet1
{
public static void main(String[] args) throws Exception
{
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()

.uri(URI.create("https://www.google.com/search?q=kau+university&as_eq=FCIT&as_f
iletype=pdf&num=2"))
.setHeader("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5
Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Mobile
Safari/537.36")
.build();

//HttpResponse<String> response = client.send(request,


HttpResponse.BodyHandlers.ofString());
//System.out.println(response.body());
HttpResponse<Path> response = client.send(request,
HttpResponse.BodyHandlers.ofFile(Paths.get("body_original.html")));
} }
Example9: HTTPGet1 output 97
Example10: HTTPGet2 98
• In this example we will communicate with the web server
weather.visualcrossing.com. It provides weather information for
clients. Its URL is https://weather.visualcrossing.com

• To use this server, you should make an account to extract a


parameter called key, it is unique for each user.

• It provides many services, one of them is the forecast of the following


15 days. The URL of this service:
https://weather.visualcrossing.com/VisualCrossingWebServices/rest/se
rvices/timeline/jeddah

• It has specific parameters, and we will use the following:

• unitGroup=metric (Units to measure temperature and wind speed)

• key=5MGJ7ZFDHFN4GD4EASEPJYCDW (of my account)


Example10: HTTPGet2 99
Example10: HTTPGet2 10
import java.io.*;
import java.net.*;
0
import java.nio.charset.*;
import java.nio.file.*;
import java.net.http.*;
import java.util.*;
public class HTTPGet2
{
public static void main(String[] args) throws Exception
{

HttpClient client = HttpClient.newHttpClient(); //London or jeddah


HttpRequest request = HttpRequest.newBuilder()
.GET()

.uri(URI.create("https://weather.visualcrossing.com/VisualCrossingWebServices/r
est/services/timeline/London?unitGroup=metric&include=days&key=5MGJ7ZFDHFN4GD4E
ASEPJYCDW&contentType=csv"))
.build();

HttpResponse<Path> response = client.send(request,


HttpResponse.BodyHandlers.ofFile(Paths.get("myWeather.csv")));
System.out.println(response.body());
// print status code to if the request fail
// 200 means successful
System.out.println(response.statusCode());
} }
Example10: HTTPGet2 output 10
1
Posting form data 10
2
• Very long query strings can look unattractive
in browsers, and older browsers and proxies
have a limit on the number of characters that
you can include in a GET request

• For that reason, a POST request is often used


for forms with a lot of data
Posting form data 10
3
• In a POST request, you do not attach
parameters to a URL

• You can add the parameters to the


HTTPRequest object

• You need to determine name/value pair for


every parameter

• You still have to URL-encode the values and


separate them with & characters
GET vs Post 10
4
• The parameters of GET are part the URL, so
they are saved in browser history and server
logs in plaintext.
• GET is less secure and easier to hack

• Using POST there is no restriction in the


length of parameters

• GET method should not be used when sending


passwords or other sensitive information
Example10: HTTPPost explanation 10
5
• The web site at
https://tools.usps.com/zipcodelookup/byaddress
contains a form to find the zip code for a street
address

• To use this form in a Java program, you need to know


the URL and the parameters of the POST request

• We will use the following parameters:


• address1=1 Market Street

• city=San Francisco

• state=CA
Example10: HTTPPost explanation 10
6
• We need to set the header field: content-
type

• It is used to indicate the resource type

• it will be assigned to the value:


application/x-www-form-urlencoded –
which represents an URL encoded form
Example10: HTTPPost page 1 10
import java.io.*;
7
import java.net.*;
import java.nio.charset.*;
import java.nio.file.*;
import java.net.http.*;
import java.util.*;
public class HTTPPost
{
public static void main(String[] args) throws
Exception
{
// form parameters
var builder = new StringBuilder();
builder.append("address1="+ "1+Market+Street" +"&");
builder.append("city="+ "San+Francisco" +"&");
builder.append("state=" + "CA");
Example10: HTTPPost page 2 10
8
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString(builder.toString()))
.uri(URI.create("https://tools.usps.com/tools/app/ziplookup/zipByAddress"))
.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36")
.setHeader("Content-Type", "application/x-www-form-urlencoded")
.build();

HttpResponse<String> response = client.send(request,


HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} }
Example10: HTTPPost output 10
9
• {"resultStatus":"SUCCESS","addressList":
[companyName":"STEUART TOWER","addressLine1":"1 MARKET
ST","city":"SAN
FRANCISCO","state":"CA","zip5":"94105","zip4":"1420","carrierRo
ute":"C011","countyName":"SAN
FRANCISCO","deliveryPoint":"99","checkDigit":"6","cmar":"N“]

You might also like