Write a program to demonstrate the ProxyClass and the ProxySelector Class. import java.net.*; import java.io.*; public class ProxyDemo { public static void main(String[] args) { // Configure the proxy (e.g., using system properties or a custom configuration) System.setProperty("http.proxyHost", "your_proxy_host"); System.setProperty("http.proxyPort", "your_proxy_port"); // Create a URL object URL url = new URL("http://example.com"); // Create a ProxySelector and set the proxy ProxySelector selector = ProxySelector.getDefault(); List
proxies = selector.select(url.toURI()); if (proxies.size() > 0) { Proxy proxy = proxies.get(0); // Use the proxy for network operations URLConnection connection = url.openConnection(proxy); // Perform network operations (e.g., read input stream, write output stream) } else { // No proxy found, use direct connection URLConnection connection = url.openConnection(); // Perform network operations } } } URL URI URL is an acronym for Uniform Resource Locator. URI is an acronym for Uniform Resource Identifier. URL is used to describe the identity of an item. URI provides a technique for defining the identity of an item. URL links a web pages, a component of a web page or a program on a web page with the help of accessing methods like protocols. URL is used to distinguish one resources from other regardless of the method used. URL provides the details about what types of protocol is to be used. URI doesn’t the protocol specifications. URL is a type of URL URI is the superset of URL. An example of an URL is https://www.javatpoint.com. An example of a URI can be ISBN 0-486-35557-4. The scheme of URL is usually a protocol such as HTTP, HTTPS, FTP, etc. The URI scheme can be protocol, designation, specification, or anything. write a program to construct URI class and URL class. import java.net.*; public class URIandURLDemo { public static void main(String[] args) { // Constructing URI objects URI uri1 = URI.create("http://www.example.com"); URI uri2 = URI.create("https://api.example.com/users/123"); URI uri3 = URI.create("mailto:[email protected]"); // Accessing URI components System.out.println("URI 1 Scheme: " + uri1.getScheme()); System.out.println("URI 1 Host: " + uri1.getHost()); System.out.println("URI 1 Path: " + uri1.getPath()); System.out.println("URI 1 Query: " + uri1.getQuery()); System.out.println("URI 1 Fragment: " + uri1.getFragment()); // Constructing URL objects URL url1 = new URL("http://www.example.com"); URL url2 = new URL ("https://api.example.com/users/123 ?param1=value1& param2=value2"); // Accessing URL components System.out.println("URL 1 Protocol: " + url1.getProtocol()); System.out.println("URL 1 Host: " + url1.getHost()); System.out.println("URL 1 Port: " + url1.getPort()); System.out.println("URL 1 Path: " + url1.getPath()); System.out.println("URL 1 Query: " + url1.getQuery()); // Converting between URI and URL URL urlFromURI = uri1.toURL(); URI uriFromURL = url1.toURI(); } } what is the use of InetAddress class? what are the types. Explain Inet4address and Inet5Address class with example. The InetAddress class in Java represents an IP address. It provides methods for creating, manipulating, and comparing IP addresses. Creating IP addresses from strings or byte arrays. Getting the host name associated with an IP address. Getting the IP address associated with a host name. Comparing IP addresses. Determining if an IP address is a loopback address or a multicast address. Types: IPv4 (Internet Protocol version 4): The older version, using 32 bits to represent an address. IPv6 (Internet Protocol version 6): The newer version, using 128 bits to represent an address. Inet4Address import java.net.*; public class Inet4AddressExample { public static void main(String[] args) { try { // Create an IPv4 address from a dotted-decimal string InetAddress address = InetAddress.getByName("192.168.1.100"); // Check if it's an Inet4Address if (address instanceof Inet4Address) { Inet4Address inet4Address = (Inet4Address) address; // Get the IPv4 address as a byte array byte[] bytes = inet4Address.getAddress(); // Convert the IPv4 address to a dotted-decimal string String dottedDecimal = inet4Address.getHostAddress(); System.out.println("IPv4 Address: " + dottedDecimal); } else { System.out.println("Not an IPv4 address."); } } catch (UnknownHostException e) { e.printStackTrace(); } } } Inet6Address import java.net.*; public class Inet6AddressExample { public static void main(String[] args) { try { // Create an IPv6 address from a hexadecimal string InetAddress address = InetAddress.getByName("2001:0db8:85a3:0000:0000:8a2e:0370:7334 "); // Check if the address is an IPv6 address if (address instanceof Inet6Address) { Inet6Address inet6Address = (Inet6Address) address; // Get the IPv6 address as a byte array byte[] bytes = inet6Address.getAddress(); // Convert the IPv6 address to a hexadecimal string String hexString = inet6Address.toString(); System.out.println("IPv6 Address: " + hexString); } else { System.out.println("Not an IPv6 address."); } } catch (UnknownHostException e) { e.printStackTrace(); } } } Define Client Server Model. Write a program to demonstrate Server side and client side connection for chat bot. The client-server model is a distributed computing architecture where a central server provides services to multiple clients. Clients request services from the server, and the server processes the requests and sends back responses. Server Side: import java.net.*; import java.io.*; public class ChatbotServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8080); // Replace 8080 with your desired port System.out.println("Chatbot server listening on port 8080..."); while (true) { Socket socket = serverSocket.accept(); System.out.println("Client connected: " + socket.getInetAddress()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Received from client: " + inputLine); // Process the input and generate a response (replace with your chatbot logic) String response = "Hello from the chatbot!"; out.println(response); } socket.close(); } } } Client Side: import java.net.*; import java.io.*; public class ChatbotClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 8080); // Replace "localhost" with the server's IP address System.out.println("Connected to chatbot server"); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true); Scanner scanner = new Scanner(System.in); while (true) { System.out.print("Enter your message: "); String message = scanner.nextLine(); out.println(message); String response = in.readLine(); System.out.println("Received from chatbot: " + response); } }} What are the HTTP methods? Write a program to print the HTTP header. HTTP Methods List HTTP (Hypertext Transfer Protocol) defines several methods to interact with web resources. Each method has a specific purpose and usage: GET: Retrieves data from a specified resource. It's typically used to request web pages, images, or other data. Example : GET /products HTTP/1.1 Host: www.example.com Accept: application/json POST: Submits data to a specified resource, often used to create new resources or update existing ones. Example: POST /products HTTP/1.1 Host: www.example.com Content-Type: application/json Content-Length: 52 {"name": "New Product", "price": 9.99} PUT: Replaces all current representations of a resource with a new one. PATCH: Applies partial updates to a resource. DELETE: Removes a specified resource. Example: DELETE /products/123 HTTP/1.1 Host: www.example.com HEAD: Similar to GET, but only retrieves the header information of a resource without the body. OPTIONS: Returns the supported methods for a specified resource. TRACE: Performs a diagnostic test on the path to a resource. CONNECT: Converts an HTTP connection into a TCP/IP tunnel. HTTP 1.1 200 OK Date: Wed, 11th August 2024 2:00:00 GMT Server: Apache/1.3.4(Unix) PHP/3.0.6 mod_perl/1.17 Last-Modified: Sun,14 August 2024 16:30:00 GMT ETag: “28d907-657-375aa2299” Accept-Ranges: bytes Content-Length: 1623 Connection : Close import java.net.*; import java.util.*; public class PrintHttpHeaders { public static void main(String[] args) throws IOException { if (args.length == 0) { System.out.println("Usage: java PrintHttpHeaders "); System.exit(1); } String urlString = args[0]; URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); System.out.println("HTTP Response Code: " + responseCode); Map> headers = connection.getHeaderFields(); for (Map.Entry> entry : headers.entrySet()) { String headerName = entry.getKey(); List headerValues = entry.getValue(); if (headerName != null) { // Check for null header name System.out.print(headerName + ": "); for (String headerValue : headerValues) { System.out.print(headerValue + " "); } System.out.println(); } } } }