使用URLConnection获取网页信息的基本流程

  参考自core java v2, chapter3 Networking.

注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apache HttpClient是两种主要的客户端实现方式,google官方推荐使用HttpURLConnection.


使用URL类可以简单获取网页信息,

URL url = new URL("http://www.baidu.com");
InputStream is = url.openStream();
Scanner sc = new Scanner(is);
但URLConnection提供了更为强大的功能,其基本步骤如下:

1. Call the openConnection method of the URL class to obtain the URLConnection object:
URLConnection connection = url.openConnection();


2. Set any request properties, using the methods
setDoInput
setDoOutput
setIfModifiedSince
setUseCaches
setAllowUserInteraction
setRequestProperty
setConnectTimeout
setReadTimeout
We discuss these methods later in this section and in the API notes.


3. Connect to the remote resource by calling the connect method.
connection.connect();
Besides making a socket connection to the server, this method also queries the server for header information.


4. After connecting to the server, you can query the header information. Two methods, getHeaderFieldKey and
getHeaderField, enumerate all fields of the header. The method getHeaderFields gets a standard Map object
containing the header fields. For your convenience, the following methods query standard fields:
getContentType
getContentLength
getContentEncoding
getDate
getExpiration
getLastModified


5. Finally, you can access the resource data. Use the getInputStream method to obtain an input stream for reading the
information. (This is the same input stream that the openStream method of the URL class returns.) The other method,
getContent, isn’t very useful in practice. The objects that are returned by standard content types such as
text/plain and image/gif require classes in the com.sun hierarchy for processing. You could register your own
content handlers, but we do not discuss that technique in this book.


package com.ljh.corejava;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class URLConnectionTest {
	public static void main(String[] args) {
		
		try{
			//1、创建URLConnction.		
			URL url = new URL("http://www.baidu.com");
			URLConnection connection = url.openConnection();
		
			//2、设置connection的属性
			connection.setConnectTimeout(10000);
			connection.setReadTimeout(10000);
			
			//3.连接
			connection.connect();
			
			//4.获取头部信息之一:获取所有头部信息后再遍历
			Map> headers = connection.getHeaderFields();
			for(Map.Entry> entry : headers.entrySet()){
				System.out.println(entry.getKey()+" : ");
				for(String value : entry.getValue()){
					System.out.println(value+" , ");
				}
			}
			

			//4.获取头部信息之二:使用简便方法
	         System.out.println("----------");
	         System.out.println("getContentType: " + connection.getContentType());
	         System.out.println("getContentLength: " + connection.getContentLength());
	         System.out.println("getContentEncoding: " + connection.getContentEncoding());
	         System.out.println("getDate: " + connection.getDate());
	         System.out.println("getExpiration: " + connection.getExpiration());
	         System.out.println("getLastModifed: " + connection.getLastModified());
	         System.out.println("----------");
	         
	         //5.获取内容
	         InputStream is = connection.getInputStream();
	         Scanner sc = new Scanner(is);
	         while(sc.hasNextLine()){
	        	 System.out.println(sc.nextLine());
	         }
	         
	         sc.close();
	         is.close();
		
		}catch(IOException e){
			e.printStackTrace();
		}

	}
}
输出结果如下:

null : 
HTTP/1.1 200 OK , 
Expires : 
Sat, 12 Oct 2013 16:16:20 GMT , 
Set-Cookie : 
H_PS_PSSID=; path=/; domain=.baidu.com , 
BDSVRTM=0; path=/ , 
BAIDUID=2CD6C90F50267C4DD25F1DA90D209AB5:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com , 
Connection : 
Keep-Alive , 
Server : 
BWS/1.0 , 
Cache-Control : 
private , 
Date : 
Sat, 12 Oct 2013 16:16:51 GMT , 
BDQID : 
0xad03b06b916405ea , 
Vary : 
Accept-Encoding , 
Transfer-Encoding : 
chunked , 
P3P : 
CP=" OTI DSP COR IVA OUR IND COM " , 
BDPAGETYPE : 
1 , 
Content-Type : 
text/html;charset=utf-8 , 
BDUSERID : 
0 , 
----------
getContentType: text/html;charset=utf-8
getContentLength: -1
getContentEncoding: null
getDate: 1381594611000
getExpiration: 1381594580000
getLastModifed: 0
----------
百度一下,你就知道




你可能感兴趣的:(C6_ANDROID)