Network Programming Chapter5Da5
Network Programming Chapter5Da5
Network Programming
Lecture 1
Course Details
5.1 Opening url Connection
5.2 Reading Data From Server
5.3 Reading Header: Retrieving specific Header Fields
and Retrieving Arbitrary Header Fields
5.4 Cache: Web Cache for Java
5.5 Configuring the connection: protected URL url,
protected boolean connected, protected boolean
doOutput, protected boolean ifModificationSince,
protected boolean useCaches and Timeouts
NETWORK PROGRAMMING
Lecture 1
Course Details
5.6 Configuring the client Request HTTP header
NETWORK PROGRAMMING
Lecture 5
5.7 Security Considerations for URLConnections
When working with URLConnections in Java, there are several
security considerations to keep in mind to ensure the safety of your
application and data. Here are some important points to consider:
NETWORK PROGRAMMING
Lecture 5
3. Input Validation and Sanitization: When constructing URLs or
handling user input, validate and sanitize the data to prevent
injection attacks such as cross-site scripting (XSS) or SQL
injection.
NETWORK PROGRAMMING
Lecture 5
7 Input and Output Stream Handling: Be cautious when reading
data from a URLConnection's input stream or writing data to its
output stream.
8 Access Control: Ensure that your application has appropriate
access controls in place.
NETWORK PROGRAMMING
Lecture 5
5.8 Guessing MIME Media Types
Guessing MIME media types in Java can be done using the
URLConnection class or by utilizing third-party libraries like:
1. URLConnection class
2. Apache Tika
3. MediaType
NETWORK PROGRAMMING
Lecture 5
URLConnection class:
NETWORK PROGRAMMING
Lecture 5
5.9 HttpURLConnection: The request Methods, Disconnecting from the
server, Handling Server Responses, Proxieses and streaming Mode
HttpURLConnection
The HttpURLConnection class in Java provides a way to send
HTTP requests to a server and receive responses. It supports
various request methods, allows for disconnection from the
server, handles server responses, and also provides options for
working with proxies and streaming mode. Let's go through
each of these aspects in detail:
NETWORK PROGRAMMING
Lecture 5
Request Methods:
HttpURLConnection supports different HTTP methods, including GET,
POST, PUT, DELETE, HEAD, OPTIONS, and more. You can set the
request method using the setRequestMethod() method. Here's an example
of making a GET request:
Disconnecting from the Server:
After sending a request and receiving a response, it's important to
disconnect from the server to release resources. You can disconnect by
calling the disconnect() method on the HttpURLConnection object. Here's
an example:
Handling Server Responses:
Once you've sent a request, you can read the server's response using
various methods provided by HttpURLConnection. For example, you can
get the response code using getResponseCode(), read the response message
using getResponseMessage(), and retrieve the response body using
getInputStream() or getOutputStream() depending on the type of
response. Here's an example of reading the response body
NETWORK PROGRAMMING
Lecture 5
Proxies:
If you need to work with a proxy server, you can set the proxy information
using the setProxy() method of the HttpURLConnection object. Here's an
example:
Streaming Mode:
HttpURLConnection supports streaming mode, which allows you to upload
or download large amounts of data in chunks. You can enable streaming
mode by setting the ChunkedStreamingMode property and then using the
getOutputStream() method to write data. Here's an example of uploading
data in streaming mode:
NETWORK PROGRAMMING
Lecture 5
Assignments
1. Explain all the connections headers with example.
NETWORK PROGRAMMING
Thank You
The End