Java Network-URL & amp; URI, java and url

Source: Internet
Author: User
Tags gopher

Java Network-URL & URI, java url

The relationship between URL and URI has been briefly described in the second part of this series: java Network-basic web concepts.

Here we repeat one point, that is, URI contains a URL, or URI is a parent class, and URL is a subclass concept.

This article introduces these two concepts.

I. URL

Java/net/URL. java

public final class URL implements Serializable {    private static final long serialVersionUID = -7627629688361524110L;    private static URLStreamHandlerFactory streamHandlerFactory;    /** Cache of protocols to their handlers */    private static final Hashtable<String, URLStreamHandler> streamHandlers            = new Hashtable<String, URLStreamHandler>();    private String protocol;    private String authority;    private String host;    private int port = -1;    private String file;    private String ref;

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

So we can use String to briefly describe a URL.

However, considering that a URL is a class, it may be more useful.

This object includes, mode, host name, port, path, identifier (ref ). Look at the source code above. That's all!

Let's look at the protocols supported by virtual machines. You can write the following test programs:

public class ProtocolTester implements IOperator {    @Override    public void start() {        testProtocol("http://www.adc.org");        testProtocol("https://www.amazon.com");        testProtocol("ftp://metalab.unc.edu");        testProtocol("telent://dibner.poly.edu/");        testProtocol("mailto:joyfulmath@163.com");        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 void testProtocol(String url) {        try {            URL u = new URL(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 composition:

The URL consists of five parts,

Mode, also known as Protocol

Authorized Institution

Path

Fragment identifier, ref

Query string

Give URL http://www.ibiblio.org/javafaq/books/jnp/index.html? Isbn = 1565229 # toc

Mode http

Authorized Institution www.ibiblio.org

Path: javafaq/books/jnp/index.html

Fragment identifier, ref toc

Query string: isbn = 1565229

Authorization organizations can be further divided into: user information, host, port.

Anmin@www.baidu.com: 8080

public class UrlValueTester implements ITestOperator {    @Override    public void startTest() {        try {            URL u = new URL("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 void method(URL u) throws InvocationTargetException, 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());            }        }    }

Get all get methods through reflection, and then obtain each attribute in sequence.

Ii. URLEncoder & URLDecoder

The URL contains the following characters:

Uppercase letters

Lowercase letters

Number 0-9

Punctuation -_.!~ *'

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

Other content must be encoded. The URL is not encoded by default.

Public class EncoderTester implements ITestOperator {public static final String DEFAULT_ENCODE = "UTF-8"; @ Override public void startTest () {try {String str = URLEncoder. encode ("This is a + base string my meal", DEFAULT_ENCODE); TraceLog. I (str); str = URLDecoder. decode (str, DEFAULT_ENCODE); TraceLog. I (str);} catch (UnsupportedEncodingException e) {e. printStackTrace ();}}}
16:47:20-28. 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 shown in the preceding figure, spaces are encoded as +. Therefore, all unspecified characters must be encoded!

For special symbols, check the situation and determine whether to encode them!

Iii. URI

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

The URI format is divided into three parts:

Scheme: sheeme-part: fragment

URI top = new URI("http://www.example.com/");URI relative  = new URI("image/logo.png");URI resolved = top.resolve(relative);output:http://www.example.com/image/logo.png"

URI can convert absolute paths and relative paths, but the URL must be an absolute path.

 

Related Article

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.