Java URL handling


 URL (Uniform Resource Locator) Chinese called a Uniform Resource Locator, sometimes referred to as a web page address. Expressed as a resource on the Internet, such as Web or FTP address.

URL can be divided into several parts as follows:

protocol://host:port/path?query#fragment

 

protocol (protocol) can be HTTP, HTTPS, FTP and File, port is the port number, path is the file path and file name.

 

URL parsing:

  • Protocol (protocol): http
  • Host (host: port): www.baidu.com
  • Port number (port): 80, did not specify the instance above URL port, because HTTP protocol default port number is 80.
  • File path (path): / index.html
  • Parameter request (query): language = cn
  • Positioning location (fragment): j2se, to pages as the id attribute of the HTML element j2se position.

 

 

URL class method

java.net package defines the URL class, processing contents related to the URL. To create and use the URL class, described separately below.

java.net.URL provides a wealth of URL built, and resources can be obtained by java.net.URL.

URL in a class contains many methods for accessing the various portions of the URL, specific methods are described below:

Examples

package pkg2020华南虎;

import java.net.*;
import java.io.*;

/**
 *
 * @author yl
 */
public class URLDemo {

    public static void main(String[] args) {
        try {
            URL url = new URL("https://i-beta.cnblogs.com/posts/edit;postId=12299874");
            System.out.println("URL为:" + url.toString());
            System.out.println("协议为:" + url.getProtocol());
            System.out.println("验证信息:" + url.getAuthority());
            System.out.println("文件名及请求参数:" + url.getFile());
            System.out.println("主机名:" + url.getHost());
            System.out.println("路径:" + url.getPath());
            System.out.println("端口:" + url.getPort());
            System.out.println("默认端口:" + url.getDefaultPort());
            System.out.println("请求参数:" + url.getQuery());
            System.out.println("定位位置:" + url.getRef());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 

结果:

run:
URL为:https://i-beta.cnblogs.com/posts/edit;postId=12299874
协议为:https
验证信息:i-beta.cnblogs.com
文件名及请求参数:/posts/edit;postId=12299874
主机名:i-beta.cnblogs.com
路径:/posts/edit;postId=12299874
端口:-1
默认端口:443
请求参数:null
定位位置:null
成功构建 (总时间: 0 秒)

 

URLConnections类方法  

openConnection()返回一个java.net.URLConnection.

例如:

  • 如果你连接HTTP协议的URL,openConnection()方法返回HttpURLConnection对象。
  • 如果你连接的URL为一个JAR文件,openConnection()方法将返回JarURLConnection对象。

实例:

package pkg2020华南虎;

import java.net.*;
import java.io.*;

/**
 *
 * @author yl
 */
public class URLConnDemo {

    public static void main(String[] args) {
        try {
            URL url = new URL("http://www.baidu.com");
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection connection = null;
            if (urlConnection instanceof HttpURLConnection) {
                connection = (HttpURLConnection) urlConnection;
            } else {
                System.out.println("请输入URL地址");
                return;
            }
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream())
            );
            String urlString = "";
            String current;
            while ((current = in.readLine()) != null) {
                urlString += current;
            }
            System.out.println(urlString);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

会输出百度首页。


 

Guess you like

Origin www.cnblogs.com/2020yl/p/12299874.html