基于apache的HttpComponents子项目抓取网页内容的HttpClientBrow...

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;

public class HttpClientBrowser {
	
	private static final ProtocolVersion PROTOCOL_VERSION = new ProtocolVersion("HTTP", 1, 1);
	
	public static int timeout=15000;
	
	private HashMap<String, String> COOKIE = new HashMap<String, String>();
	
	public void getcookie(HttpResponse conn) {
		String cookieVal = null;
		for(Header bfh:conn.getAllHeaders()){
			if (bfh.getName().equalsIgnoreCase("set-cookie")) {
				cookieVal=bfh.getValue();
				String[] keys = cookieVal.split(";");
				for (int k = 0; k < keys.length; k++) {
					String[] vls = keys[k].split("=");
					if (",Path,Max-Age,Domain,Comment,".indexOf(vls[0])==-1&&vls.length>1) {
						COOKIE.put(vls[0], vls[1]);
					}
				}
			}
		}
	}
	
	public String cookie2String() {
		String retn = "";
		for (String key : COOKIE.keySet()) {
			retn += key + "=" + COOKIE.get(key) + ";";
		}
		return retn;
	}
	
	public String getHttpSessionId() {
		return COOKIE.get("JSESSIONID");
	}
	
	private HttpResponse post(String url,ArrayList<NameValuePair> list) throws ClientProtocolException, IOException,Exception{
		HttpPost httpPost = new HttpPost(url);
		String cookie = cookie2String();
		if (cookie.length() > 0)	httpPost.setHeader("Cookie", cookie);
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params,timeout);
		HttpConnectionParams.setSoTimeout(params,timeout);
		if(list!=null){
			httpPost.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
		}
		HttpProtocolParams.setVersion(params, PROTOCOL_VERSION);
		httpPost.setParams(params);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse response =httpClient.execute(httpPost);
		int code = response.getStatusLine().getStatusCode();
		
		if(code!=HttpStatus.SC_OK){
			System.out.println("HTTP状态返回码:"+code);
			System.out.println("HTTP返回内容:"+EntityUtils.toString(response.getEntity(), "gb2312"));
			throw new Exception("HTTP访问出错!");			
		}
		getcookie(response);
		return response;
	}
	
	public HttpResponse post(String url,String[][] params) throws ClientProtocolException, IOException,Exception{
		ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();	
		for(int i=0;i<params.length;i++){
			if(params[i].length>=2){
				list.add(new BasicNameValuePair(params[i][0],params[i][1]));
			}
		}
		return post(url,list);
	}
	
	public HttpResponse get(String url) throws ClientProtocolException, IOException,Exception{
		HttpGet httpGet = new HttpGet(url);
		String cookie = cookie2String();
		if (cookie.length() > 0)	httpGet.setHeader("Cookie", cookie);
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params,timeout);
		HttpConnectionParams.setSoTimeout(params,timeout);
		HttpProtocolParams.setVersion(params, PROTOCOL_VERSION);
		httpGet.setParams(params);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse response =httpClient.execute(httpGet);
		int code = response.getStatusLine().getStatusCode();
		
		if(code!=HttpStatus.SC_OK){
			System.out.println("HTTP状态返回码:"+code);
			System.out.println("HTTP返回内容:"+EntityUtils.toString(response.getEntity(), "gb2312"));
			throw new Exception("HTTP访问出错!");			
		}
		getcookie(response);
		return response;
	}
	
	public String html(String url) throws ClientProtocolException, IOException,Exception{
		HttpResponse res = get(url);
		return EntityUtils.toString(res.getEntity());
	}
	
	public String html(String url,String encode) throws ClientProtocolException, IOException,Exception{
		HttpResponse res = get(url);
		return EntityUtils.toString(res.getEntity(),encode);
	}
	
	public String html(String url,String[][] params) throws ClientProtocolException, IOException,Exception{
		HttpResponse res = post(url,params);
		return EntityUtils.toString(res.getEntity());
	}
	
	public String html(String url,String[][] params,String encode) throws ClientProtocolException, IOException,Exception{
		HttpResponse res = post(url,params);
		return EntityUtils.toString(res.getEntity(),encode);
	}
	
}
//同一个HttpClientBrowser对象对于url的多次访问,保持同一个session访问。

你可能感兴趣的:(httpclient)