होस्ट नाम निम्न सिंटैक्स से मेल खाना चाहिए:
hostname = domainlabel [ "." ] | 1*( domainlabel "." ) toplabel [ "." ]
domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
toplabel = alpha | alpha *( alphanum | "-" ) alphanum
जैसा कि आप देख सकते हैं, केवल .और -अनुमति है, _नहीं है।
फिर आप कहते हैं कि //5-12-145-35_s-81:443इसकी अनुमति है, और यह मेजबान नाम के लिए नहीं है ।
यह देखने के लिए कि कैसे पैन निकलता है:
URI uriBadHost = URI.create("//5-12-145-35_s-81:443");
System.out.println("uri = " + uriBadHost);
System.out.println(" authority = " + uriBadHost.getAuthority());
System.out.println(" host = " + uriBadHost.getHost());
System.out.println(" port = " + uriBadHost.getPort());
URI uriGoodHost = URI.create("//example.com:443");
System.out.println("uri = " + uriGoodHost);
System.out.println(" authority = " + uriGoodHost.getAuthority());
System.out.println(" host = " + uriGoodHost.getHost());
System.out.println(" port = " + uriGoodHost.getPort());
उत्पादन
uri = //5-12-145-35_s-81:443
authority = 5-12-145-35_s-81:443
host = null
port = -1
uri = //example.com:443
authority = example.com:443
host = example.com
port = 443
जैसा कि आप देख सकते हैं, जब authorityएक मान्य होस्ट नाम होता है, hostऔर portपार्स किया जाता है, लेकिन जब मान्य नहीं होता है, तो authorityइसे फ्रीफ़ॉर्म टेक्स्ट के रूप में माना जाता है, और आगे कोई पार्स नहीं किया जाता है।
अपडेट करें
टिप्पणी से:
System.out.println( new URI(null, null, "/5-12-145-35_s-81", 443, null, null, null))आउटपुट: /// 5-12-145-35_s-81: 443। मैं इसे hostname के रूप में दे रहा हूं
जिस URIकंस्ट्रक्टर को आप बुला रहे हैं वह एक सुविधा विधि है, और यह सरल पूर्ण यूआरआई स्ट्रिंग बनाता है और फिर इसे पार्स करता है।
पासिंग "5-12-145-35_s-81", 443बन जाता है //5-12-145-35_s-81:443।
पासिंग "/5-12-145-35_s-81", 443बन जाता है ///5-12-145-35_s-81:443।
पहले में, यह एक मेजबान और बंदरगाह है , और पार्स करने में विफल रहता है।
दूसरे में अधिकार भाग खाली है, और /5-12-145-35_s-81:443एक मार्ग है ।
URI uri1 = new URI(null, null, "/5-12-145-35_s-81", 443, null, null, null);
System.out.println("uri = " + uri1);
System.out.println(" authority = " + uri1.getAuthority());
System.out.println(" host = " + uri1.getHost());
System.out.println(" port = " + uri1.getPort());
System.out.println(" path = " + uri1.getPath());
उत्पादन
uri = ///5-12-145-35_s-81:443
authority = null
host = null
port = -1
path = /5-12-145-35_s-81:443