The java.net.URL class encapsulates URL addresses and provides basic methods for resolving URL addresses, such as obtaining the URL host name and port number. Java.net.URLConnection represents a communication link between an application and a URL that can be used to read and write resources referenced by this URL.

  • URI = Universal Resource Identifier, uniform resource identifier
  • URL = Universal Resource Locator, uniform resource locator
  • URN = Universal Resource Name, the name of a unified resource

Uris can be urls and UrNs, or a combination of urls and UrNs (with Locator and Name). URN is like the name of the person, THE URL is like the address of the person. In other words: The URN establishes the identity, and the URL provides the means to find it.

A URL is actually a resource

URL represents a resource, it uses the unified resource location format, such as: juejin. Cn, in fact, we can also use URL to construct the object, using a string? No, there are a number of constructors in Java that allow strings to be used to specify urls. With a URL object, you can retrieve the contents of the URL in several different ways. Yes, I’m talking about web pages:

import java.net.*;
import java.io.*;
public class IntTest{
    public static void main(String [] args) throws Exception{
        URL url = new URL(args[0]);
        InputStream html = url.openStream();
        int c;
        do{
            c = html.read();
            if(c ! = -1){
                System.out.print((char) c); }}while(c ! = -1); }}Copy the code

Perhaps you haven’t heard of this before: URLConnection

Generally static operations are very easy, using a URL object to read a URL is not too easy, just said the static is my own, in fact, there is no such word, but I think of the data structure, such as reading operations are static, so I also do the same metaphor 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. This function can return a URLConnection object. The URLConnection object returns an URLConnection subclass: HttpURLConnection;

import java.net.*;
import java.io.*;
public class Handin{
    public static void main(String [] args) throws Exception{
        URL url=new URL(""http//com.aaaaa.com""); // The url was made up
        HttpURLConnection con=(HttpURLConnection)url.openConnection();
        int c;
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setrequestMethod("POST");
        con.setRequestProperty("Content-type"."application/X-WWW-form-urlencoded");
        con.connect();
        PrintWriter pri=new PrintWriter{
        new OutputStreamWriter{con.getOutputStream(),"12321".true}
        pri.print("sasd"+URLEncoder.encode(args[0] +'dasda'));
        pri.flush();
        System.out.println(con.getResponseMessage());
        InputStream in=con.getInputStream();
        do{
            char x;
            c=in.read();
            x=(char)c;
            if(c! = -1){ System.out.print(x); }}while(c! = -1); }}Copy the code

For HttpURLConnection, the only way to get an HttpURLConnection object is to use http://URL and call the openConnection function of the HttpURLConnection object. It then assigns the returned URLConnection value to HttpURLConnection.

Who’s behind this?

Both URL and URLConnection are assigned to URLStreamHandler and URLConnection. After encapsulation, the URL object checks its URL protocol port and calls an object called URLStreamHandlerFactory. This object is used to subclass URLStreamHandler to conform to the specified protocol.

Subclass URLStreamHandler to create a corresponding URLConnection object that also parses the URL, so you can define the custom URL format. The object URLConnection usually deals with the server.

import java.net.*;
import java.io.*;
public class TimeURLConnection extends URLConnection{
    private Socket con;
    public final static int de=13;
    public TimeURLConnection void connect(a) throws IOException{
        if(! =connected){int port=url.getPort();
            if(port<0) port=de;
            con=new Socket(url.getHost(),port);
            connected=true; }}public synchronized InputStream getInputStream(a) throws IOException{
        connect();
        returncon.getInputStream(); }}Copy the code