---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
URL类
构造方法:URL(String spec)
根据 String 表示形式创建 URL 对象
URL(String protocol, String host, int port, String file)
根据指定 protocol、host、port 号和 file 创建 URL 对象。
....
http://www.baidu.com:80/index.php?key=nihao&n=88
String getFile() index.php?key=nihao&n=88
获取此 URL 的文件名。
String getHost() www.baidu.com
获取此 URL 的主机名(如果适用)。
String getPath() index.php
获取此 URL 的路径部分。
int getPort() 80
获取此 URL 的端口号。若URL中无指定端口,则返回-1,所以当返回-1的时候,通常会指定常用端口80
String getProtocol() http
获取此 URL 的协议名称。
String getQuery() key=nihao&n=88
获取此 URL 的查询部分。
URLConnection openConnection() 用这个的时候就不需要使用Socket了,其实它内部封装了Socket
返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
URLConnection下的方法:
InputStream getInputStream()
返回从此打开的连接读取的输入流。
很多头信息还有其他信息都可以通过URLConnection对象获取
代码示例:
URL u = new URL("http://www.baidu.com/s?wd=love");
URLConnection uc = u.openConnection();
InputStream in = uc.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1)
{
System.out.println(new String(buf,0,len));
}
---------------------- ASP.Net+Android+IOS开发、 .Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net