Clasa Java.net.URI în Java
Această clasă oferă metode pentru crearea instanțelor URI din componentele sale sau prin analizarea formei șirului acelor componente pentru accesarea și preluarea diferitelor componente ale unei instanțe URI. Ce este URI? URI înseamnă Uniform Resource Identifier. Un identificator uniform de resurse este o secvență de caractere utilizată pentru identificarea unei anumite resurse. Permite interacțiunea reprezentării resursei în rețea folosind protocoale specifice.
URI URL și URN: Care este diferența?
Întrebarea care trebuie să vă vină în minte trebuie să fie că, dacă URI identifică o resursă, ce face URL-ul? Oamenii folosesc adesea termenii în mod interschimbabil, dar nu este corect. Conform 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. Sintaxa URI:
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 #fragid1Constructori:
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 2396În mod similar, alți constructori sunt furnizați pe baza componentelor cunoscute la momentul creării.
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 partMetode:
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)Parametri: uri : URI a relativiza
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()Implementare Java: 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 ()); } }
Ieșire: 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()Implementarea Java : 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 ()); } }
Ieșire: 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=1496918039682Referinte: Documentație oficială Java ase scrie comentarii daca gasesti ceva incorect sau vrei sa impartasesti mai multe informatii despre subiectul discutat mai sus. Creați un test