Java Network Programming (iii) Creating and using URLs to access resources on the network

Source: Internet
Author: User
Tags ftp site java se

Link Address: http://www.cnblogs.com/mengdd/archive/2013/03/09/2951877.html

Create and use URLs to access resources on the network

  A URL (Uniform Resource Locator) is the short name of a Uniform Resource Locator that represents the address of a resource on the Internet.

Through the URL we can access various network resources on the Internet, such as the most common WWW, ftp site. The browser can find the appropriate file or other resource on the network by parsing the given URL.

In the most widely used TCP/IP, the resolution of the host name in the URL is also a standard of the protocol, the so-called Domain name resolution service .

Using URLs for network programming does not require much understanding of the protocol itself, and is relatively simple in terms of functionality.

URL composition

A URL consists of two main sections:

  protocol Identifiers : HTTP, FTP, file, and so on.

  Resource Name : Host name, file name, port number, reference.

Create URL

In a Java program, you can create a URL object that represents a URL address.

  The URL object represents an absolute URL address, but the URL object can be constructed with an absolute URL, a relative URL, and a partial URL.

The code to create the URL is as follows, and throws an exception if the creation fails:

      Try      {         url myurl = new URL ("http://www.google.com.tw/");      }      catch (malformedurlexception e)      {         //exception handler code here      }

Get the individual properties of a URL object

There are various methods in the URL class to get Properties:

Getprotocol

GetHost

Getport

GetFile

GetRef

The example procedure is as follows:

get URL Object Properties

Create and use URLs to access online resources

To get the actual bit or content information for the URL, use its openconnection () method to create an URLConnection object from it, related to the call URL object, which returns a URLConnection object. It may throw an IOException exception.

  URLConnection is a general-purpose class that accesses remote resource properties. If you establish a connection to a remote server, you can use URLConnection to check the properties of the remote object before transferring it to local. These properties are defined by the HTTP protocol specification and are only meaningful for URL objects that use the HTTP protocol.

The URL and URLConnection classes are good for simple programs that want to establish a connection to the HTTP server to get information.

The example program UrlConnection01, establishes the connection, obtains the input stream from the connection object, then reads in, then writes out to the file.

program UrlConnection01

You can also get the input stream directly from the URL object, see Example program URLCONNECTION02.

program UrlConnection02

See the source code to see the internal implementation mechanism is the same:

    
{ return OpenConnection (). getInputStream (); }

The program code URLCONNECTION03 reads the site content in a character stream and displays it on the console.

program UrlConnection03

Resources

Santhiya Garden Zhang Long teacher Java SE Series video tutorial.

Java Network Programming (iii) Creating and using URLs to access resources on the network

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.