Java Network programming 2-url and URIs

Source: Internet
Author: User

//Create URL can specify the requested URL protocol, but different JVM support protocols may not be the same (most support HTTP, file, HTTPS)
The //construct only determines that the protocol support in the string is not supported, and does not determine the correctness of the URL
URL url=new url ("http://www.baidu.com");
System.out.println (Url.getprotocol ());
System.out.println (Url.gethost ());

returns 1 if no port specified in//url
System.out.println (Url.getport ());
The default port for the protocol is not specified in//url, and no default port for that protocol is returned-1
System.out.println (Url.getdefaultport ());


//Difference
Url.getfile (); //Return path information with query parameters
Url.getpath (); //return path only


//Gets the contents of the URL (the HTTP protocol does not contain header content or other protocol content)
InputStream input= Url.openstream ();
BufferedReader reader=new BufferedReader (new InputStreamReader (Input, "utf-8")); //Different URLs may not encode the same
String Tmp=null;
while ((Tmp=reader.readline ())!=null) {
SYSTEM.OUT.PRINTLN (TMP);
}


Gets the underlying connection object in the URL, which can be used to get more information and operations
//For example, the direct URL gets the content default does not time out, this may cause the program blocking, through the connection object can set the timeout time
URLConnection connection= url.openconnection ();


//Encode a special string in a URL
Urldecoder.decode ("xxxx", "utf-8");
Urlencoder.encode ("xxxx", "utf-8");

URI does not have network get function

Uri uri=new uri ("http://www.baidu.com");
System.out.println (Uri.gethost ());
System.out.println (Uri.getport ());

Java Network programming 2-url and URIs

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.