Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Network Programming Chapter5Da5

The document discusses various topics related to network programming in Java including opening URL connections, reading data from servers, HTTP headers, caching, configuring connections, security considerations, guessing MIME types, and using HttpURLConnection. It provides code examples and explanations of key concepts.

Uploaded by

Bikash Sah
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)
12 views

Network Programming Chapter5Da5

The document discusses various topics related to network programming in Java including opening URL connections, reading data from servers, HTTP headers, caching, configuring connections, security considerations, guessing MIME types, and using HttpURLConnection. It provides code examples and explanations of key concepts.

Uploaded by

Bikash Sah
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/ 13

GOOD MORNING!!!

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

5.7 Security Considerations for URLConnections


5.8 Guessing MIME Media Types
5.9 HttpURLConnection: The request Methods,
Disconnecting from the server, Handling Server
Responses, Proxieses and streaming Mode

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:

1 SSL/TLS Encryption: If your application communicates with a


remote server over HTTPS, ensure that the connection is
encrypted using SSL/TLS protocols.
2 Certificate Validation: Validate the server's SSL certificate to
ensure it is issued by a trusted Certificate Authority (CA) and
matches the domain you are connecting to.

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.

4 Connection Timeouts: Set appropriate timeouts for establishing


connections and reading data from the remote server.
5 Restricted Protocols: Restrict the usage of insecure protocols like
FTP or plain HTTP. Avoid using the "ftp" or "http" protocols
when possible, and prefer secure alternatives like "sftp" or
"https."

6 Secure Authentication: If your application requires


authentication, ensure that the credentials are sent securely.

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.

9 Keep Libraries Updated: Regularly update the Java libraries


and dependencies used in your application to benefit from security
patches and fixes provided by the library developers.

10 Logging and Monitoring: Implement proper logging and


monitoring mechanisms to track any suspicious activities or
potential security incidents related to URLConnections.

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:

The URLConnection class in Java is a fundamental class that represents a


connection to a resource on the internet. It is part of the java.net package
and provides an abstract representation of a connection to a URL.

Using Apache Tika:


Apache Tika is a powerful Java library for content type detection and
extraction. You can include the Apache Tika library in your project and use
it to guess MIME media types.
Using MediaType (guava library):
MediaType is a class from the Google Guava library that provides
functionality for handling media types, including guessing the MIME type.

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.

2. Write a program for HTTP headers


connection from URL =
“https://www.google.com.np/imghp?hl=en&ogbl
3. What is HTTP Headers?
4. Explain all HttpURLConnection

NETWORK PROGRAMMING
Thank You

The End

You might also like