Java+Demo URL类的简单使用

记录一下java url类的简单使用
URL(Uniform Resource Locator,统一资源定位器)简单理解就是网上的某资源的地址,比如https://blog.csdn.net
在java中的URL类型,主要设计四个参数 protocol(协议名):常见的就是http和https,host(主机名):通常说的域名,port(端口号):java的URL类中默认是-1 实际上我们都知道直接域名或IP访问是80端口,file(文件名):就是域名后面的那部分,是访问资源的目录

新建一个URL常见的构造方法

public URL(String protocol, String host, int port, String file)
    throws MalformedURLException{}

public URL(String protocol, String host, String file)
        throws MalformedURLException {}
        
public URL(String protocol, String host, int port, String file,
           URLStreamHandler handler) throws MalformedURLException {}       

用的最多的还是直接用url

public URL(String spec) throws MalformedURLException {}

例如

URL url = new URL("https://blog.csdn.net");
URL url = new URL("https://blog.csdn.net/");
URL url = new URL("https://blog.csdn.net/myyhtw");
URL url = new URL("https://blog.csdn.net/myyhtw/article/details/114041042");
URL url = new URL("http://localhost:8080/#/login");
URL url = new URL("https://editor.csdn.net/md?not_checkout=1&articleId=929775980");
import java.net.MalformedURLException;
import java.net.URL;

public class URLMainDemo {
    public static void main(String[] args){
        try {
            URL url = new URL("https://blog.csdn.net");
            System.out.println("协议:"+ url.getProtocol());
            System.out.println("主机:"+ url.getHost());
            System.out.println("端口号:"+ url.getPort());
            System.out.println("文件名:"+ url.getFile());
            System.out.println("路径:"  + url.getPath());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

结果

协议:https
主机:blog.csdn.net
端口号:-1
文件名:
路径:

在url末尾加上"/"

try {
    URL url = new URL("https://blog.csdn.net/");
    System.out.println("协议:"+ url.getProtocol());
    System.out.println("主机:"+ url.getHost());
    System.out.println("端口号:"+ url.getPort());
    System.out.println("文件名:"+ url.getFile());
    System.out.println("路径:"  + url.getPath());
} catch (MalformedURLException e) {
    e.printStackTrace();
}

结果

协议:https
主机:blog.csdn.net
端口号:-1
文件名:/
路径:/

可以看到两者的区别在于文件名和路径,那就是说域名之后的/开始就是文件名也是路径

try {
    URL url = new URL("https://blog.csdn.net/myyhtw/article/details/114041042");
    System.out.println("协议:"+ url.getProtocol());
    System.out.println("主机:"+ url.getHost());
    System.out.println("端口号:"+ url.getPort());
    System.out.println("文件名:"+ url.getFile());
    System.out.println("路径:"  + url.getPath());
    System.out.println("锚点:"    + url.getRef());
    System.out.println("查询条件:"+url.getQuery());
} catch (MalformedURLException e) {
    e.printStackTrace();
}

运行结果

协议:https
主机:blog.csdn.net
端口号:-1
文件名:/myyhtw/article/details/114041042
路径:/myyhtw/article/details/114041042
锚点:null
查询条件:null

public static void main(String[] args){
    try {
        URL url = new URL("https://editor.csdn.net/md?not_checkout=1&articleId=929775980");
        System.out.println("协议:"+ url.getProtocol());
        System.out.println("主机:"+ url.getHost());
        System.out.println("端口号:"+ url.getPort());
        System.out.println("文件名:"+ url.getFile());
        System.out.println("路径:"  + url.getPath());
        System.out.println("锚点:"    + url.getRef());
        System.out.println("查询条件:"+url.getQuery());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

运行结果

协议:https
主机:editor.csdn.net
端口号:-1
文件名:/md?not_checkout=1&articleId=929775980
路径:/md
锚点:null
查询条件:not_checkout=1&articleId=929775980

这里可以看到当有查询条件后,路径和文件名就不是一致的了,问号后面的是查询条件 问号前面的是路径

public static void main(String[] args){
    try {
        URL url = new URL("http://localhost:8080/myyhtw/#/login");
        System.out.println("协议:"+ url.getProtocol());
        System.out.println("主机:"+ url.getHost());
        System.out.println("端口号:"+ url.getPort());
        System.out.println("文件名:"+ url.getFile());
        System.out.println("路径:"  + url.getPath());
        System.out.println("锚点:"    + url.getRef());
        System.out.println("查询条件:"+url.getQuery());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

运行结果

协议:http
主机:localhost
端口号:8080
文件名:/myyhtw/
路径:/myyhtw/
锚点:/login
查询条件:null

锚点就是#号后面的 端口号这里不再是默认-1

使用URL类一般要配合其他类使用

 URL url = new URL(urlStr);// 获得网络资源的URL
 HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 获得连接对象
URL url=new URL(urlStr);
URLConnection urlConnection=url.openConnection();

URLConnection类

public abstract class URLConnection {
}

HttpURLConnection 类

abstract public class HttpURLConnection extends URLConnection {
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个简单Java使用WebService的示例: 1. 创建一个Java项目并在项目中添加以下依赖: ```xml <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.3.1</version> </dependency> ``` 2. 创建一个Java类作为我们的WebService客户端: ```java import javax.xml.namespace.QName; import javax.xml.ws.Service; import java.net.URL; public class HelloWorldClient { public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:8080/HelloWorld?wsdl"); QName qname = new QName("http://webservice.example.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.sayHello("World")); } } ``` 3. 创建一个Java接口作为我们的WebService服务: ```java import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface HelloWorld { @WebMethod String sayHello(String name); } ``` 4. 创建一个Java类作为我们的WebService服务实现: ```java import javax.jws.WebService; @WebService(endpointInterface = "com.example.webservice.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { return "Hello " + name + "!"; } } ``` 5. 在Eclipse中运行我们的WebService服务端 6. 在Eclipse中运行我们的WebService客户端 7. 我们应该能够在控制台看到以下输出: ``` Hello World! ``` 这就是一个简单Java使用WebService的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值