HttpClient的Post和Get訪問網頁

1、基礎JAR包

Mavenproject下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>

2、源碼

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; /** * 用戶本身定義消息頭。html

*/ private Map<String, String> _headers; /** * 使用默認客戶端對象。java

*/ public HttpClient() { // 1. 建立HttpClient對象。apache

_httpclient = new DefaultHttpClient(); logger.info("create _httpclient ..."); } /** * 調用者指定客戶端對象。 * * @param httpclient */ public HttpClient(Map<String, String> headers) { // 1. 建立HttpClient對象。dom

_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. 無論運行方法是否成功。都必須釋放鏈接。ide

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>(); // 組裝參數。post

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 ..."); } }this


3、測試演示樣例

package com.qidou.httpclient;

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

/**
 * HTTPclient測試類。
 */
public class TestHttpClient 
{
	/**
	 * HTTP請求client。

*/ private HttpClient httpClient; /** * 初始化HTTP請求client。 */ { 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請求client。可以本身設置消息頭的一些參數 : httpClient = new HttpClient(headers); httpClient = new HttpClient(); } /** * 測試方法。 * 實現過程: * 一、使用username/密碼登錄人人網 : http://www.renren.com/PLogin.do * 二、訪問博客 : 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 = "。。。url

。"; String password = "。。。3d

。"; /** * 登錄參數。日誌

*/ 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請求client。 httpClient.shutdown(); } }


4、執行結果

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 ...
相關文章
相關標籤/搜索