|NO.Z.00118|——————————|BigDataEnd|——|Java&网络编程.V07|——|Java.v07|url类|概念使用|

一、[URL类的概念和使用]:URL类(熟悉)
### --- 基本概念

——>        java.net.URL(Uniform Resource Identifier)类主要用于表示统一的资源定位器,
——>        也就是指向万维网上“资源”的指针。这个资源可以是简单的文件或目录,
——>        也可以是对复杂对象的引用,例如对数据库或搜索引擎的查询等。
——>        通过URL可以访问万维网上的网络资源,最常见的就是www和ftp站点,
——>        浏览器通过解析给定的URL可以在网络上查找相应的资源。
——>        URL的基本结构如下:
——>            <传输协议>://<主机名>:<端口号>/<资源地址>
二、常用的方法
方法声明 功能介绍
URL(String spec)  根据参数指定的字符串信息构造对象
String getProtocol() 获取协议名称
String getHost() 获取主机名称
int getPort()  获取端口号
String getPath()  获取路径信息
String getFile() 获取文件名
URLConnection openConnection()  获取URLConnection类的实例
三、URLConnection类
### --- 基本概念

——>        java.net.URLConnection类是个抽象类,
——>        该类表示应用程序和URL之间的通信链接的所有类的超类,
——>        主要实现类有支持HTTP特有功能的HttpURLConnection类。
四、HttpURLConnection类的常用方法
方法声明 功能介绍
InputStream getInputStream() 获取输入流
void disconnect() 断开连接
五、编程代码
package com.yanqi.task19;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLTest {

    public static void main(String[] args) {

        try {
            // 1.使用参数指定的字符串来构造对象
            URL url = new URL("https://www.yanqi.com/");
            // 2.获取相关信息并打印出来
            System.out.println("获取到的协议名称是:" + url.getProtocol());
            System.out.println("获取到的主机名称是:" + url.getHost());
            System.out.println("获取到的端口号是:" + url.getPort());

            // 3.建立连接并读取相关信息打印出来
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String str = null;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }
            br.close();
            // 断开连接
            urlConnection.disconnect();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
六、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=54752:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task19.URLTest
获取到的协议名称是:https
获取到的主机名称是:www.yanqi.com
获取到的端口号是:-1

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(22)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示