网络编程01

2014-11-24 02:03:57 · 作者: · 浏览: 11

1、基于http的网络编程

URL、URLConnection、HttpURLConnection、JarURLConnection

URL 统一资源定位器

创建URL连接用法:

首先要声明一个URL对象,定位资源地址

1、通过在URL 上调用openConnection 方法创建连接对象。

2、处理设置参数和一般请求属性。

3、使用connect 方法建立到远程对象的实际连接。

4、远程对象变为可用。远程对象的头字段和内容变为可访问。

public static void test01(){

URL url = null;

try {

url = new URL("http://192.168.0.12:8080/mp3/resources.xml");

URLConnection urlc = url.openConnection();

urlc.connect();

StringBuffer sb = new StringBuffer();

BufferedReader br =

new BufferedReader(new InputStreamReader(urlc.getInputStream()));

String str = "";

while((str = br.readLine()) != null){

sb.append(str);

}

System.out.print(sb.toString());

} catch (MalformedURLException e) {

e.printStackTrace();

}catch (IOException e) {

e.printStackTrace();

}

}