Java网络编程之URL、URLConnection、URLEncoder、URLDecoder

在java的网络编程中让我们获取一个网络地址的资源时,我们常用到的是URL统一资源定位符(Uniform Resource Locator),URL类java.net.URL。其URL定义如下:

[ scheme :] scheme-specific-part[ # fragment]

其中,方括号 [...] 用于描述可选组成部分,字符 :# 代表它们自身。详细信息查看其API。


一、 URL

其主要方法有如下:

    1、构造方法

         URL(String spec)    根据 String 表示形式创建 URL 对象。
         URL(String protocol, String host, int port, String file)  根据指定 protocol、host、port 号和 file 创建 URL 对象。 

    2、常用的方法

         URLConnection openConnection()   返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
         InputStream openStream()   打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
  

   例如读取百度首页的数据:

 

package andy.network.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

/**  
 * @author Zhang,Tianyou  
 * version:2014-11-24 下午4:14:20  
 * 
 *  
 */

public class URLTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		URL url = new URL("http://www.baidu.com");
		//url 打开获取一个输入流
		InputStream in = url.openStream();
		//实例化一个scanner
		Scanner scanner = new Scanner(in);
		//设置读取分隔符
		scanner.useDelimiter("\n");
		while (scanner.hasNext()) {
			String str = scanner.next();
			System.out.println(str);
		}

	}

}

二、URLConnection

       URLConnection是封装了访问远程资源的类,通过该类可以建立远程服务器的连接以及获取和检查远程资源的属性。

       其含有HttpURLConnection, JarURLConnection 两个子类。

      其主要有如下的方法:

  1、构造方法

     protected  URLConnection(URL url)  构造一个到指定 URL 的 URL 连接。 

   2、常用方法
     InputStream getInputStream()   返回从此打开的连接读取的输入流。
     OutputStream getOutputStream()     返回写入到此连接的输出流。
     void setConnectTimeout(int timeout)   设置一个指定的超时值(以毫秒为单位),该值将在打开到此 URLConnection 引用的资源的通信链接时使用。
    int getContentLength()  返回 content-length 头字段的值。
    String getContentType()   返回 content-type 头字段的值 

   使用实例如下:
  

package andy.network.test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;

/**  
 * @author Zhang,Tianyou  
 * version:2014-11-24 下午4:33:19  
 * 
 *  
 */

public class URLConnectionTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		URL url = new URL("http://www.baidu.com");
		
		//获取URLConnection
		URLConnection conn = url.openConnection();
		//设置超时时间 超过2秒 就超时
		conn.setReadTimeout(2000);
		
		System.out.println("获取内容大小:" + conn.getContentLength());
		System.out.println("获取内容类型:" + conn.getContentType());

	}

}

三、URLEncoder和URLDncoder

  URLEncoder和URLDncoder主要是完成内容的编码和解码操作。

 还有如下方法:
     static String encode(String s, String enc)    使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式。
     static String decode(String s, String enc)    使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码。


使用如下:

package andy.network.test;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**  
 * @author Zhang,Tianyou  
 * version:2014-11-24 下午4:42:47  
 * 
 *  
 */

public class URLEncoderAndURLDecoder {

	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// 解码和编码的格式都应该相同
		String coder = "UTF-8";
		String src = "hello,安卓 Android";
		
		String enStr = URLEncoder.encode(src, coder);
		String deStr = URLDecoder.decode(enStr, coder);
				
		System.out.println("编码后内容:" + enStr);
		System.out.println("解码后内容:" + deStr);
	}

}

执行结果为:

编码后内容:hello%EF%BC%8C%E5%AE%89%E5%8D%93+Android
解码后内容:hello,安卓 Android


你可能感兴趣的:(url,网络编程,urlconnection,URLEncoder,URLDecoder)