URL的常用方法

来源:互联网 发布:mac dare you和diva 编辑:程序博客网 时间:2024/06/06 05:15

URL的常用方法

String path ="http://localhost:8080/myweb/mail.html?name=kk";

URL url =new URL(path);//同一资源定位符

System.out.println(url.getProtocol());//得到协议
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());//得到资源路径 /myweb/mail.html
System.out.println(url.getFile());// /myweb/mail.html?name=kk

//和服务器端连接
URLConnection conn = url.openConnection();

//读取服务器返回的mail.html页面的数据
InputStream in = conn.getInputStream();
byte[] arr = new byte[1024];
int len = in.read(arr);
System.out.println(new String(arr,0,len));

 

0 0
原创粉丝点击