URI URL och URN: Vad är skillnaden?
Frågan som du måste tänka på måste vara att om URI identifierar en resurs vad gör URL? Människor använder ofta termerna omväxlande men det är inte korrekt. Enligt Tim Berners Lee 'A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource.' 'A URI can be further classified as a locator a name or both. The term 'Uniform Resource Locator' (URL) refers to the subset of URI that identify resources via a representation of their primary access mechanism (e.g. their network 'location') rather than identifying the resource by name or by some other attribute(s) of that resource. The term 'Uniform Resource Name' (URN) refers to the subset of URI that are required to remain globally unique and persistent even when the resource ceases to exist or becomes unavailable.' For examplehttps://www.geeksforgeeks.org/java/java//url-class-java-examples/Represents a URL as it tells the exact location where url class article can be found over the network.
url-class-java-examplesRepresents a URN as it does not tell anything about the location but only gives a unique name to the resource. The difference between an object of URI class and an URL class lies in the fact that a URI string is parsed only with consideration of syntax and no lookups of host is performed on creation. Comparison of two URI objects is done solely on the characters contained in the string. But on the other hand a URL string is parsed with a certain scheme and comparisons of two URL objects is performed by looking of actual resource on the net. URI-syntax:
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
abc: //admin:[email protected]:1234/path/data ?key=value&key2=value2#fragid1
abc:// admin:[email protected]:1234 /path/data ?key=value&key2=value2#fragid1
abc://admin:[email protected]:1234/ path/data ?key=value&key2=value2#fragid1
abc://admin:[email protected]:1234/path/data ?key=value&key2=value2 #fragid1
abc://admin:[email protected]:1234/path/data ?key=value&key2=value2 #fragid1Konstruktörer:
Syntax : public URI(String str) throws URISyntaxException Parameters : str : String to be parsed into URI Throws : URISyntaxException : If the string violates RFC 2396
Syntax : public URI(String scheme String ssp String fragment) throws URISyntaxException Parameters : scheme : scheme name ssp : scheme-specific part fragment : fragment part Throws : URISyntaxException : If the URI string constructed from the given components violates RFC 2396På liknande sätt tillhandahålls andra konstruktörer baserat på vilka komponenter som är kända vid skapandet.
Syntax : public URI(String scheme String userInfo String host int port String path String query String fragment) Parameters : scheme : string representing scheme userInfo : userinfo of URI host : host component of URI port : listening port number path : path of URI query : String representing the query part fragment :optional fragment
Syntax : public URI(String scheme String host String path String fragment) Parameters : scheme : string representing scheme host : host component of URI path : path of URI fragment :optional fragment
Syntax : public URI(String scheme String authority String path String query String fragment) Parameters : scheme : string representing scheme authority : authority path : path of URI query : String representing the query partMetoder:
Syntax : public static URI create(String str) Parameters : str : String to be parsed as URI
Syntax : public URI parseServerAuthority()
Syntax : public URI normalize()
Syntax : public URI resolve(URI uri) Parameters : uri : URI to be resolvedAnother overloaded method which takes string as argument and is equivalent to calling resolve(URI.create(str)).
Syntax : public URI resolve(String str) Parameters : str : String to be parsed as URI
Syntax : public URI relativize(URI uri)Parametrar: uri : URI för att relativisera
Syntax : public URL toURL() throws MalformedURLException Throws : MalformedURLException : If error occurs while constructing URL
Syntax : public String getScheme()
Syntax : public String getRawSchemeSpecificPart()
Syntax : public String getSchemeSpecificPart()
Syntax : public String getRawAuthority()
Syntax : public String getAuthority()
Syntax : public String getRawUserInfo()
Syntax : public String getUserInfo()Java-implementering: Java
// Java program to illustrate various // URI class methods import java.net.*; class uridemo1 { public static void main(String[] args) throws Exception { String uribase = 'https://www.geeksforgeeks.org/'; String urirelative = 'languages/../java'; String str = 'https://www.google.co.in/?gws_rd=ssl#'+'' + 'q=networking+in+java+geeksforgeeks'+'' +'&spf=1496918039682'; // Constructor to create a new URI // by parsing the string URI uriBase = new URI(uribase); // create() method URI uri = URI.create(str); // toString() method System.out.println('Base URI = ' + uriBase.toString()); URI uriRelative = new URI(urirelative); System.out.println('Relative URI = ' + uriRelative.toString()); // resolve() method URI uriResolved = uriBase.resolve(uriRelative); System.out.println('Resolved URI = ' + uriResolved.toString()); // relativized() method URI uriRelativized = uriBase.relativize(uriResolved); System.out.println('Relativized URI = ' + uriRelativized.toString()); // normalize() method System.out.println(uri.normalize().toString()); // getScheme() method System.out.println('Scheme = ' + uri.getScheme()); // getRawShemeSpecific() method System.out.println('Raw Scheme = ' + uri.getRawSchemeSpecificPart()); // getSchemeSpecificPart() method System.out.println('Scheme-specific part = ' + uri.getSchemeSpecificPart()); // getRawUserInfo() method System.out.println('Raw User Info = ' + uri.getRawUserInfo()); // getUserInfo() method System.out.println('User Info = ' + uri.getUserInfo()); // getAuthority() method System.out.println('Authority = ' + uri.getAuthority()); // getRawAuthority() method System.out.println('Raw Authority = ' + uri.getRawAuthority()); } }
Utgång: Base URI = https://www.geeksforgeeks.org/ Relative URI = languages/../java Resolved URI = https://www.geeksforgeeks.org/java/java/ Relativized URI = java https://www.google.co.in/?gws_rd=ssl#q=networking+in+ java+geeksforgeeks&spf=1496918039682 Scheme = https Raw Scheme = //www.google.co.in/?gws_rd=ssl Scheme-specific part = //www.google.co.in/?gws_rd=ssl Raw User Info = null User Info = null Authority = www.google.co.in Raw Authority = www.google.co.in
Syntax : public String getHost()
Syntax : public int getPort()
Syntax : public String getRawPath()
Syntax : public String getPath()
Syntax : public String getRawQuery()
Syntax : public String getQuery()
Syntax : public String getRawFragment()
Syntax : public String getFragment()
Syntax : public int compareTo(URI uri) Parameters : uri : URI to be compared with
Syntax : public boolean equals(Object ob) Parameters : ob : object to be compared for equality
Syntax : public boolean isAbsolute()
Syntax : public boolean isOpaque()
Syntax : public int hashCode()
Syntax : public String toString()
Syntax : public String toASCIIString()Java-implementering : Java
//Java Program to illustrate various //URI class methods import java.net.*; class uridemo1 { public static void main(String[] args) throws Exception { String str = 'https://www.google.co.in/?gws_rd=ssl#'+'' + 'q=networking+in+java+geeksforgeeks'+'' +'&spf=1496918039682'; // Constructor to create a new URI // by parsing the given string. URI uri = new URI(str); // getHost() method System.out.println('Host = ' + uri.getHost()); // getPort() method System.out.println('Port = ' + uri.getPath()); // getRawPath() method System.out.println('Raw Path = ' + uri.getRawPath()); // getPath() method System.out.println('Path = ' + uri.getPath()); // getQuery() method System.out.println('Query = ' + uri.getQuery()); // getRawQuery() method System.out.println('Raw Query = ' + uri.getRawQuery()); // getFragment() method System.out.println('Fragment = ' + uri.getFragment()); // getRawFragment() method System.out.println('Raw Fragment = ' + uri.getRawFragment()); URI uri2 = new URI(str + 'fr'); // compareTo() mrthod System.out.println('CompareTo =' + uri.compareTo(uri2)); // equals() method System.out.println('Equals = ' + uri.equals(uri2)); // hashcode() method System.out.println('Hashcode : ' + uri.hashCode()); // toString() method System.out.println('toString : ' + uri.toString()); // toASCIIString() method System.out.println('toASCIIString : ' + uri.toASCIIString()); } }
Utgång: Host = www.google.co.in Port = / Raw Path = / Path = / Query = gws_rd=ssl Raw Query = gws_rd=ssl Fragment = q=networking+in+java+geeksforgeeks&spf=1496918039682 Raw Fragment = q=networking+in+java+geeksforgeeks&spf=1496918039682 CompareTo =-2 Equals = false Hashcode : 480379574 toString : https://www.google.co.in/?gws_rd=ssl#q=networking+ in+java+geeksforgeeks&spf=1496918039682 toASCIIString : https://www.google.co.in/?gws_rd=ssl#q= networking+in+java+geeksforgeeks&spf=1496918039682Referenser: Officiell Java-dokumentation skriv kommentarer om du hittar något felaktigt eller om du vill dela mer information om ämnet som diskuterats ovan. Skapa frågesport