Java ---- URL and URLConnection

The class encapsulates the URL address and provides basic methods to resolve the URL address, such as obtaining the host name and port number of the URL. represents the communication link between the application and the URL, which can be used to read and write the resources referenced by this URL.

  • URI = Universal Resource Identifier, which is interpreted in Chinese as a unified resource identifier
  • URL = Universal Resource Locator, which is interpreted in Chinese as a unified resource locator
  • URN = Universal Resource Name, which means unified resource name in Chinese

URI can be divided into URL and URN, or a combination of URL and URN (with Locator and Name at the same time). URN is like a person's Name and URL is like a person's address. In other words: URN determines the identity, and URL provides the way to find it.

A URL is actually a resource

URL represents a resource, which uses a unified resource location format, such as: In fact, we can also use URLs to construct objects. Can we use strings? No, there are a large number of constructors in java that allow you to specify URLs one by one. With a URL object, you can obtain the content corresponding to the URL through several different methods. Yes, I'm talking about web pages:

public class IntTest{
    public static void main(String [] args) throws Exception{
        URL url = new URL(args[0]);
        InputStream html = url.openStream();
        int c;
            c =;
            if(c != -1){
                System.out.print((char) c);
        }while(c != -1);

Perhaps this is something you haven't heard of before: URLConnection

Generally, static operations are easy. It's not easy to use a URL object to read a URL. I just said that I started the static. In fact, there is no such saying. Instead, I think of data structures. Operations such as reading are static, so I make the same analogy here;

What if you want more control over HTTP transactions?, For example, we definitely need to submit some data to the server. We need the openConnection function of the URL object, which can return a URLConnection object. If the URL uses the HTTP protocol, the URLConnection object will return a subclass of URLConnection: HttpURLConnection;

public class Handin{
    public static void main(String [] args) throws Exception{
        URL url=new URL(""http//"");// The website is made up
        HttpURLConnection con=(HttpURLConnection)url.openConnection();
        int c;
        PrintWriter pri=new PrintWriter{
        new OutputStreamWriter{con.getOutputStream(),"12321",true}
        InputStream in=con.getInputStream();
            char x;

For HttpURLConnection, the HttpURLConnection object can only be obtained by using http://URL (note that I just wrote a format here) to create, call the openConnection function of the HttpURLConnection object, and then assign the returned URLConnection value to HttpURLConnection.

Who is behind it?

Both URL and URLConnection assign URLStreamHandler and URLConnection to work. After encapsulation, the URL object checks its URL protocol port and then calls an object of URLStreamHandlerFactory, which is mainly used to create URLStreamHandler subclasses to match the specified protocol.

The subclass URLStreamHandler creates a corresponding URLConnection object and also parses the URL, so that you can define the customized URL format. The object URLConnection usually deals with the server.

public class TimeURLConnection extends URLConnection{
    private Socket con;
    public final static int de=13;
    public TimeURLConnection void connect() throws IOException{
            int port=url.getPort();
            if(port<0) port=de;
            con=new Socket(url.getHost(),port);
    public synchronized InputStream getInputStream() throws IOException{
        return con.getInputStream();

Tags: Java Back-end url

Posted by Wien on Tue, 21 Sep 2021 12:44:08 +0530