HttpClient的Post和Get访问网页

一、基础JAR包

Maven工程下pom.xml需配置的jar包
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient-cache</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.1.2</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
	</dependencies>

二、源代码

package com.qidou.httpclient;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

/**
 * HTTP请求客户端,提供GET和POST两种方式发送请求。
 * 
 * @author Administrator
 *
 */
public class HttpClient 
{
	/**
	 * 日志对象。
	 */
	private static Logger logger = Logger.getLogger(HttpClient.class);
	
	/**
	 * 默认HTTP请求客户端对象。
	 */
	private DefaultHttpClient _httpclient;

	/**
	 * 用户自定义消息头。
	 */
	private Map<String, String> _headers;

	/**
	 * 使用默认客户端对象。
	 */
	public HttpClient() 
	{
		// 1. 创建HttpClient对象。
		_httpclient = new DefaultHttpClient();
		logger.info("create _httpclient ...");
	}

	/**
	 * 调用者指定客户端对象。
	 * 
	 * @param httpclient
	 */
	public HttpClient(Map<String, String> headers) 
	{
		// 1. 创建HttpClient对象。
		_httpclient = new DefaultHttpClient();
		this._headers = headers;
		logger.info("create _httpclient ...");
	}

	/**
	 * HTTP POST请求。
	 * 
	 * @param url
	 * @param params
	 * @return
	 * @throws InterruptedException
	 */
	public HttpResponse post(String url, Map<String, String> params)
	{
		// 2. 创建请求方法的实例,并指定请求URL,添加请求参数。
		HttpPost post = postForm(url, params);
		logger.info("create httppost : " + url);
		
		return invoke(post);
	}

	/**
	 * HTTP GET请求。
	 * 
	 * @param url
	 * @return
	 */
	public HttpResponse get(String url) 
	{
		HttpGet get = new HttpGet(url);
		logger.info("create httpget : " + url);
		
		return invoke(get);
	}
	
	/**
	 * 发送请求,处理响应。
	 * @param request
	 * @return
	 */
	private HttpResponse invoke(HttpUriRequest request)
	{
		if (this._headers != null) 
		{
			//
			addHeaders(request);
			logger.info("addHeaders to http ...");
		}
		
		HttpResponse response = null;
		try
		{
			// 3. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,返回一个HttpResponse。
			response = _httpclient.execute(request);
			logger.info("execute http success... ; body = " + EntityUtils.toString(response.getEntity()));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			logger.info("execute http exception...");
		}
		finally
		{
			// 4. 无论执行方法是否成功,都必须释放连接。
			request.abort();
			logger.info("release http ...");
		}
		
		return response;
	}
	
	/**
	 * 获取post方法。
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	private HttpPost postForm(String url, Map<String, String> params) 
	{
		HttpPost httpost = new HttpPost(url);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();

		// 组装参数。
		Set<String> keySet = params.keySet();
		for (String key : keySet) 
		{
			nvps.add(new BasicNameValuePair(key, params.get(key)));
		}

		try 
		{
			logger.info("set utf-8 form entity to httppost ...");
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}

		return httpost;
	}
	
	/**
	 * 增加消息头。
	 * 
	 * @param httpost
	 */
	private void addHeaders(HttpUriRequest httpost)
	{
		Iterator<Entry<String, String>> it = this._headers.entrySet()
				.iterator();
		Entry<String, String> entry = null;
		String name = null;
		String value = null;

		while (it.hasNext())
		{
			entry = it.next();
			name = entry.getKey();
			value = entry.getValue();

			httpost.addHeader(name, value);
		}
	}
	
	/**
	 * 关闭HTTP客户端链接。
	 */
	public void shutdown()
	{
		_httpclient.getConnectionManager().shutdown();
		logger.info("shutdown _httpclient ...");
	}
}

三、测试示例

package com.qidou.httpclient;

import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;

/**
 * HTTP客户端测试类。
 */
public class TestHttpClient 
{
	/**
	 * HTTP请求客户端。
	 */
	private HttpClient httpClient;
	
	/**
	 * 初始化HTTP请求客户端。
	 */
	{
		System.out.println("init http client...");
		
		Map<String,String> headers = new HashMap<String,String>();
		headers.put("Accept", "*/*");
		headers.put("Accept-Encoding", "gzip, deflate");
		headers.put("Accept-Language", "zh-cn");
		headers.put("Connection", "Keep-Alive");
		headers.put("Cookie", "UC_SID=66134277449dcc94b77dfbcdb65a3d70; cna=HnmMDIROzW4CAbdDI+iUySCj; PHPSESSID=91818085719e575f59bd5f4b31df402f; lzstat_uv=260674130547154807|3037573; lzstat_ss=582317835_2_1409680845_3037573; PHPSESSID=91818085719e575f59bd5f4b31df402f; frontvar=lns%3Dmenu-1%2Cflow-0%2Ctraf-0%2Ccont-0%2Cvisitor-0%2Ceanalysis-0%2Cbigdata-0%2C%26cmenu%3Dsite_overview%26siteShowHis%3Dopen; CNZZDATA30086426=cnzz_eid%3D2088392191-1409645088-http%253A%252F%252Ftongji.cnzz.com%252F%26ntime%3D1409650488; CNZZDATA30069868=cnzz_eid%3D241047449-1409644292-http%253A%252F%252Ftongji.cnzz.com%252F%26ntime%3D1409647196; CNZZDATA33222=cnzz_eid%3D1336832806-1409645823-http%253A%252F%252Ftongji.cnzz.com%252F%26ntime%3D1409651227; CNZZDATA30001831=cnzz_eid%3D732886644-1409644865-http%253A%252F%252Ftongji.cnzz.com%252F%26ntime%3D1409649918; _cnzz_CV30069868=%E6%98%AF%E5%90%A6%E7%99%BB%E5%BD%95%7C%E6%AD%A3%E5%B8%B8%E7%99%BB%E5%BD%95%7C1409680810339");
		headers.put("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)");
		headers.put("Referer", "http://www.baidu.com/");
		headers.put("x-requested-with", "XMLHttpRequest");
		
		//新建HTTP请求客户端,可以自己设置消息头的一些参数 : httpClient = new HttpClient(headers);
		httpClient = new HttpClient();
	}
	
	/**
	 * 测试方法。
	 * 实现过程:
	 * 1、使用用户名/密码登陆人人网 : http://www.renren.com/PLogin.do
	 * 2、访问博客 : http://blog.renren.com/blog/304317577/449470467
	 * 
	 */
	@org.junit.Test
	public void testPost()
	{
		String url = "http://www.renren.com/PLogin.do";
		String redirectURL = "http://blog.renren.com/blog/304317577/449470467";
		String userName = "。。。。";
		String password = "。。。。";
		
		/**
		 * 登陆参数。
		 */
		Map<String,String> params = new HashMap<String,String>();
		params.put("origURL", redirectURL);
		params.put("domain", "renren.com");
		params.put("isplogin", "true");
		params.put("formName", "");
		params.put("method", "");
		params.put("submit", "登录");
		params.put("email", userName);
		params.put("password", password);
		
		//发送请求登陆人人网。
		HttpResponse postResponse = httpClient.post(url,params);
		
		//获取消息头的跳转地址,跳转到指定博客。
		String redirectLocation = postResponse.getFirstHeader("Location").getValue();
		System.out.println(redirectLocation);
		
		//获取跳转博客网页。
		httpClient.get(redirectLocation);
		
		//关闭HTTP请求客户端。
		httpClient.shutdown();
	}
}

四、运行结果

init http client...
2014-09-04 18:31:48 [com.qidou.httpclient.HttpClient]-[INFO] create _httpclient ...
2014-09-04 18:31:48 [com.qidou.httpclient.HttpClient]-[INFO] set utf-8 form entity to httppost ...
2014-09-04 18:31:48 [com.qidou.httpclient.HttpClient]-[INFO] create httppost : http://www.renren.com/PLogin.do
2014-09-04 18:31:49 [org.apache.http.client.protocol.ResponseProcessCookies]-[WARN] Cookie rejected: "[version: 0][name: t][value: f28a837f39c7773213280e099b611aaf][domain: renren.com][path: /xtalk/][expiry: null]". Illegal path attribute "/xtalk/". Path of origin: "/PLogin.do"
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] execute http success... ; body = The URL has moved <a href="http://blog.renren.com/blog/304317577/449470467">here</a>


2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] release http ...
http://blog.renren.com/blog/304317577/449470467
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] create httpget : http://blog.renren.com/blog/304317577/449470467
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] execute http success... ; body = 博客网页太大略去。。。


2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] release http ...
2014-09-04 18:31:49 [com.qidou.httpclient.HttpClient]-[INFO] shutdown _httpclient ...

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。