Java distinction between URL and Uri

Source: Internet
Author: User
Java Code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://dingbuoyi.javaeye.com/blog/836621# ">
  1. String urlstring ="Http: // 192.168.21.77: 8080/SWP/mainpage? AA = 11 & BB % 3d22";
  2. Uri uri = URI. Create (urlstring );
  3. System. Out. println (URI. getpath ());
  4. System. Out. println (URI. getquery ());// Decoding
  5. URL url2 =NewURL (urlstring );
  6. System. Out. println (url2.getquery ());// Do not decode
 
String urlstring = "http: // 192.168.21.77: 8080/SWP/mainpage? AA = 11 & BB % 3d22 "; Uri uri = Uri. create (urlstring); system. out. println (URI. getpath (); system. out. println (URI. getquery (); // decoded URL url2 = new URL (urlstring); system. out. println (url2.getquery (); // do not decode

Uri Decoding

REFERENCE Note: The URI class can escape the fields in certain situations. We recommend that you use URI to manage URL encoding and decoding, and use touri () and Uri. tourl () to convert the two classes.

You can also use the urlencoder and urldecoder classes, but they are only applicable to HTML encoding, which is different from the encoding mechanism defined in rfc2396.

A reference URI is a pure syntactic structure used to specify different parts of a string that identifies a web resource. A URL is a special case of Uri. It contains sufficient information to locate web resources. Other Uris, such:
Mailto: cay@horstman.com
It does not belong to the positioning operator because no resource can be located based on the identifier. A URI like this is called urn (Uniform Resource Name ).
In the Java class library, the URI class does not contain any methods to access resources. Its only function is to parse. On the contrary, the URL class can open a stream that reaches the resource. Therefore, the URL class can only act on the modes that Java Class Libraries know how to handle, such as http:, https:, ftp:, local file system (File :), and JAR file (jar :).
One of the functions of the URI class is to parse the identifiers and break them into different components. You can read them using the following methods:
Getschema
Gethost
Getport
Getpath
Getquery

Another function of the URI class is to process absolute and relative identifiers. If the following absolute URI exists:
Http://docs.mycompany.com/api/java/net/serversocket.html
And a relative URI as follows:
../Java/NET/socket.html # socket ()
Then you can combine them into an absolute URI:
Http://docs.mycompany.com/api/java/net/socket.html#Socket ()
This process is called relative URL conversion.
The opposite process is relative. For example, suppose you have a basic URI:
Http://docs.mycompany/api
And another URI:
Http://docs.mycompany/api/java/lang/String.html
Then the relative URI is:
Java/lang/string.html

 

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.