Java Network---Re-discussion URL & URI

Source: Internet
Author: User
Tags ldap gopher

The relationship between URLs and URIs is outlined in the second article in this series: Java Network---Basic web concepts.

One thing to repeat here is that the URI contains the URL, or the URI is the parent class, and the URL is the concept of the subclass.

This article will elaborate on these 2 concepts.

One: URL

Java/net/url.java

 Public Final classUrlImplementsSerializable {Private Static Final LongSerialversionuid = -7627629688361524110l; Private Staticurlstreamhandlerfactory streamhandlerfactory; /**Cache of protocols to their handlers*/    Private Static FinalHashtable<string, urlstreamhandler>streamhandlers=NewHashtable<string, urlstreamhandler>(); PrivateString Protocol; PrivateString Authority; PrivateString host; Private intPort =-1; PrivateString file; PrivateString ref;

A URL is long like this: http://home.cnblogs.com/u/deman/

So we can use string to simply describe a URL.

But considering a URL as a class might be more useful.

This object includes, schema, hostname, port, path, identifier (ref). Look at the above source code, that is these things!

Look at the virtual machines that support those protocols, and you can write the following test programs:

 Public classProtocoltesterImplementsIoperator {@Override Public voidstart () {Testprotocol ("Http://www.adc.org"); Testprotocol ("Https://www.amazon.com"); Testprotocol ("Ftp://metalab.unc.edu"); Testprotocol ("telent://dibner.poly.edu/"); Testprotocol ("Mailto:[email protected]"); Testprotocol ("FILE:///ETC/PASSWD"); Testprotocol ("Gopher://gopher.anc.org.za"); Testprotocol ("Ladp://ldap.itd.umich.edu"); Testprotocol ("Jar:http://ldap.itd.umich.edu.jar"); Testprotocol ("Nfs://utopia.poly.edu"); Testprotocol ("Jdbc:mysql:/utopia.poly.edu"); }    Private voidtestprotocol (String url) {Try{URL u=Newurl (URL); TRACELOG.I (U.getprotocol ()+ "is supported"); } Catch(malformedurlexception e) {//e.printstacktrace ();//TRACELOG.W (E.getmessage ());String protocol = url.substring (0,url.indexof (': '))); TRACELOG.W (Protocol+ "is not supported"); }    }

URL consists of:

The URL is made up of five parts,

Patterns, also known as protocols

Authorized Agency

Path

Fragment identifier, ref

Query string

Give dot URL Http://www.ibiblio.org/javafaq/books/jnp/index.html?isbn=1565229#toc

Mode http

Authorized Agency Www.ibiblio.org

Path javafaq/books/jnp/index.html

Fragment identifier, ref TOC

Query string isbn=1565229

Authorized institutions can be further divided into: User information, host, Port.

[Email protected]:8080

 Public classUrlvaluetesterImplementsItestoperator {@Override Public voidstarttest () {Try{URL u=NewURL ("Http://tieba.baidu.com/f/fdir?fd=%E7%A7%91%E5%AD%A6%E6%8A%80%E6%9C%AF&ie=utf-8&sd=%E8%AE%A1%E7%AE %97%e6%9c%ba%e8%bd%af%e4%bb%b6 ");        Method (U); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(InvocationTargetException e) {e.printstacktrace (); }    }     Public voidMethod (URL u)throwsInvocationTargetException, illegalaccessexception {Class cls=U.getclass (); Method[] Methods=Cls.getdeclaredmethods ();  for(Method m:methods) {Try{                if(M.getname (). Contains ("Get") {tracelog.i (M.getname ()); TRACELOG.I (":"+m.invoke (u)); }            }Catch(Exception e) {TRACELOG.W (M.getname ()); }        }    }

With reflection, get all the Get methods, and then get each property in turn.

II: Urlencoder & Urldecoder

The URL uses the following characters:

Capital

lowercase letters

Number 0-9

Punctuation-_.! ~* '

Character/&? @ # ; $ + =% for special purposes.

All other content needs to be encoded, and the URL is not encoded by default

 Public classEncodertesterImplementsitestoperator{ Public Static FinalString Default_encode = "UTF-8"; @Override Public voidstarttest () {Try{String str= Urlencoder.encode ("This is a + base string I eat", Default_encode);            TRACELOG.I (str); STR=Urldecoder.decode (str, default_encode);        TRACELOG.I (str); } Catch(unsupportedencodingexception e) {e.printstacktrace (); }    }}
12-28 16:47:20.915 23343-23433/com.joyfulmath.sample.javanetwork i/encodertester:starttest:this+is+a+%2b+base+ String+%e6%88%91+%e5%90%83+%e9%a5%ad [at (encodertester.java:20)]12-28 16:47:20.916 23343-23433/ Com.joyfulmath.sample.javanetwork I/encodertester:starttest:this is a + base string I eat [at (encodertester.java:22)]

As above, the space will be encoded into +, so the non-given character, all need to encode!

and special symbols, you need to see the situation, determine whether to encode!

Three. URI

The URI is the content of the pure string and is the relative content of the URL.

The URI format is divided into three parts:

Scheme:sheeme-part:fragment

New URI ("http://www.example.com/"); Uri relative  new uri ("image/logo.png"= top.resolve (relative); Output:http:   www.example.com/image/logo.png "

URIs can be converted to absolute paths and relative paths, but URLs must be absolute paths.

Java Network---Re-discussion URL & URI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.