Feedback Form

Key Classes: InetAddress, URL, URI, HttpURLConnection, and CookieManager

Java Networking Key Classes Explained (InetAddress, URL, URI, HttpURLConnection, CookieManager)

Java में Networking एक बहुत important concept है, जो client और server के बीच communication को enable करता है। जब हम internet या local network पर data भेजते या प्राप्त करते हैं, तो Java की कुछ core classes इसका foundation बनती हैं। इस blog में हम InetAddress, URL, URI, HttpURLConnection, और CookieManager जैसी important classes को detail में समझेंगे — ताकि college exams और practical coding दोनों में ये topic crystal clear हो जाए।

Introduction to Java Networking Classes

Java के java.net package में वो सारी classes और interfaces मौजूद हैं जो network communication handle करते हैं। इन classes की मदद से हम IP addresses, URLs, HTTP requests, responses और cookies तक को manage कर सकते हैं। नीचे हम step by step हर class को practical examples के साथ समझेंगे।

InetAddress Class

InetAddress class का use किसी host (जैसे computer या website) का IP address पता करने के लिए किया जाता है। यह DNS (Domain Name System) के जरिए domain name को IP address में convert करती है।

Key Points of InetAddress

  • यह host name और IP address दोनों represent करती है।
  • यह static methods provide करती है जिससे हम IP address find कर सकते हैं।
  • इस class को directly instantiate नहीं किया जा सकता।

Important Methods of InetAddress

Method Description
getLocalHost() Local system का IP address return करता है।
getByName(String host) दिए गए host name का IP address देता है।
getHostName() IP address के लिए host name देता है।
getHostAddress() IP address को String के रूप में return करता है।

Example (InetAddress)

import java.net.*; public class IPExample { public static void main(String[] args){ try{ InetAddress ip = InetAddress.getByName("www.google.com"); System.out.println("Host Name: " + ip.getHostName()); System.out.println("IP Address: " + ip.getHostAddress()); } catch(Exception e){ System.out.println(e); } } }

ऊपर के code में हमने Google का IP address print किया। इसी तरह आप किसी भी domain का IP पता कर सकते हैं।

URL Class

URL (Uniform Resource Locator) class का use किसी web resource (जैसे image, web page, file आदि) को uniquely identify करने के लिए किया जाता है। यह protocol, host name, port, और file path को represent करती है।

Structure of a URL

एक URL चार main parts से बनता है:

  • Protocol: जैसे http, https, ftp आदि।
  • Host Name: Website का domain जैसे example.com।
  • Port: Optional number जो server से connect करने के लिए होता है।
  • File Path: Resource की exact location।

Example of URL

https://www.example.com:443/docs/tutorial.html

यहाँ https protocol है, example.com host है, 443 port है और /docs/tutorial.html resource path है।

Important Methods of URL Class

Method Description
getProtocol() URL का protocol return करता है।
getHost() Host name return करता है।
getPort() Port number return करता है।
getFile() File name और path return करता है।
openConnection() URLConnection object return करता है जिससे network communication possible होता है।

Example (URL Class)

import java.net.*; public class URLExample { public static void main(String[] args) throws Exception { URL url = new URL("https://www.example.com:443/docs/tutorial.html"); System.out.println("Protocol: " + url.getProtocol()); System.out.println("Host: " + url.getHost()); System.out.println("Port: " + url.getPort()); System.out.println("File: " + url.getFile()); } }

URI Class

URI (Uniform Resource Identifier) class, URL से closely related है लेकिन ये केवल identifier represent करती है — जरूरी नहीं कि वो actual resource exist करे।

Difference Between URL and URI

Parameter URL URI
Definition Web resource को locate करता है। सिर्फ resource को identify करता है।
Usage Connection establish करने के लिए। Identification purpose के लिए।
Includes Protocol Yes No

Example (URI Class)

import java.net.*; public class URIExample { public static void main(String[] args) throws Exception { URI uri = new URI("https://www.example.com/index.html"); System.out.println("Scheme: " + uri.getScheme()); System.out.println("Host: " + uri.getHost()); System.out.println("Path: " + uri.getPath()); } }

यह class तब useful होती है जब हमें किसी resource को सिर्फ identify करना हो, access नहीं करना।

HttpURLConnection Class

HttpURLConnection class Java में HTTP requests और responses को handle करने के लिए इस्तेमाल होती है। जब आप किसी web server से data भेजते या प्राप्त करते हैं (जैसे API calls), तो ये class काम में आती है।

Key Features of HttpURLConnection

  • HTTP methods जैसे GET, POST, PUT, DELETE को support करता है।
  • Request headers और body को handle कर सकता है।
  • Response code और message को retrieve कर सकता है।

Important Methods

Method Description
setRequestMethod(String method) HTTP request method सेट करता है (जैसे GET या POST)।
getResponseCode() Server response code (जैसे 200, 404) return करता है।
getInputStream() Server से data पढ़ने के लिए input stream देता है।

Example (HttpURLConnection)

import java.io.*; import java.net.*; public class HttpExample { public static void main(String[] args) { try { URL url = new URL("https://jsonplaceholder.typicode.com/posts/1"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); System.out.println("Response Code: " + conn.getResponseCode()); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); } catch (Exception e) { e.printStackTrace(); } } }

यह code server से JSON data प्राप्त करता है और उसे console पर print करता है।

CookieManager Class

जब हम किसी website से interact करते हैं, तो browser cookies को manage करता है ताकि sessions maintain रह सकें। Java में CookieManager class इसी काम के लिए होती है। यह cookies को store, retrieve और send करने की facility देती है।

Features of CookieManager

  • Automatic cookie handling करता है।
  • Cookie storage policy define की जा सकती है।
  • Client और server के बीच secure session maintain करता है।

Example (CookieManager)

import java.net.*; public class CookieExample { public static void main(String[] args) throws Exception { CookieManager cookieManager = new CookieManager(); CookieHandler.setDefault(cookieManager); System.out.println("Cookie Manager set successfully!"); } }

ऊपर के code से एक global cookie manager set होता है जो future HTTP requests में cookies को manage करेगा।

Summary Table of Networking Classes

Class Name Purpose Package
InetAddress Host name और IP address handle करता है। java.net
URL Web resource को locate करता है। java.net
URI Resource को identify करता है। java.net
HttpURLConnection HTTP communication handle करता है। java.net
CookieManager Cookies को manage करता है। java.net

Exam-Oriented Notes (Quick Revision)

  • InetAddress: Hostname और IP address find करने के लिए।
  • URL: किसी resource की full address specify करने के लिए।
  • URI: केवल resource की पहचान बताता है, location नहीं।
  • HttpURLConnection: Server के साथ HTTP connection बनाने के लिए।
  • CookieManager: Cookies को handle करने के लिए।
  • Package: ये सभी classes java.net package में होती हैं।
  • Real-life Example: जब आप browser में कोई site खोलते हैं, Java background में इन्हीं classes का use करता है।