URLConnection类

在java.net包中定义了URLConnection类,该类用来表示与URL创建的通讯链接。URLConnection类的实例经过调用URL类的openConnection()方法得到。URLConnection类用于访问网络资源的主要方法以下所示。html

void addRequestProperty(String key, String value):      java

添加由键值对指定的通常请求属性。网络

abstract void connect():                                      url

 打开到此URL引用的资源的通讯连接(若是还没有创建这样的链接)。spa

Object getContent():                                                             .net

检索此URL链接的内容。code

long getDate():                                                                      htm

返回date头字段的值。资源

boolean getDefaultUseCaches():                                       get

返回URLConnection的useCaches标志的默认值。

InputStream getInputStream():                                          

 返回今后打开的链接读取的输入流。

OutputStream getOutputStream():                                    

返回写入到此链接的输出流。

URL getURL():                                                                         

返回此URLConnection的URL字段的值。

boolean getUseCaches():                                                    

返回此URLConnection的useCaches字段的值。

import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.net.URL;  
import java.net.URLConnection;  
public class Test {  
    public static void main(String args[]) throws Exception {  
        URL url = new URL("http://www.263.net/index.html");//建立URL  
        URLConnection uc = url.openConnection();    //得到URLConnection  
          
        //读取URL链接的网络资源  
        BufferedReader in = new BufferedReader(new InputStreamReader(uc.   
        getInputStream()));  
        String line;  
        while ((line = in.readLine()) != null) {  
            System.out.println(line);  
        }  
        in.close();  
    }  
}
相关文章
相关标签/搜索