The java.net.URL 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. java.net.URLConnection 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: https://juejin.cn 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:
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); } }
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;
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 website is 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); } }
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.
import java.net.*; import java.io.*; public class TimeURLConnection extends URLConnection{ private Socket con; public final static int de=13; public TimeURLConnection void connect() 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() throws IOException{ connect(); return con.getInputStream(); } }