httpclient妙用二 httpclient保持会话登录

httpclient保持会话登录

 

1.原理

    httpclient会话保持,原理是维持session会话,而session的保持是通过cookie来维持的。

    本次要讲的是,得到用户cookie后,免登录,用HttpClient 保持原来session访问原本一定要登录才能做的事。

    详细原理可参见http://abandontofree.blog.sohu.com/273547680.html

    网上有个比较好的例子,参见http://www.cnblogs.com/yaowen/p/3757571.html

 

2.maven工程中引入对httpclient的依赖

		<properties>
			<httpclient.version>4.3.2</httpclient.version>
		<properties>
		<dependencyManagement>
			<dependency>
				<groupId>commons-logging</groupId>
				<artifactId>commons-logging</artifactId>
				<version>1.1.1</version>
			</dependency>
			<dependencies>
					<!-- httpclient -->
			<dependency>
				<groupId>org.apache.httpcomponents</groupId>
				<artifactId>httpclient</artifactId>
				<version>${httpclient.version}</version>
				<exclusions>
					<exclusion>
						<groupId>commons-logging</groupId>
						<artifactId>commons-logging</artifactId>
					</exclusion>
				</exclusions>
			</dependency>

			<dependency>
				<groupId>org.apache.httpcomponents</groupId>
				<artifactId>fluent-hc</artifactId>
				<version>${httpclient.version}</version>
				<exclusions>
					<exclusion>
						<groupId>commons-logging</groupId>
						<artifactId>commons-logging</artifactId>
					</exclusion>
				</exclusions>
			</dependency>

			<dependency>
				<groupId>org.apache.httpcomponents</groupId>
				<artifactId>http-core</artifactId>
				<version>${httpclient.version}</version>
				<exclusions>
					<exclusion>
						<groupId>commons-logging</groupId>
						<artifactId>commons-logging</artifactId>
					</exclusion>
				</exclusions>
			</dependency>
		</dependencies>
	</dependencyManagement>

 

3.第一种情况:每次登录后才进行后续操作

   这种情况不用去处理cookie,httpclient4.x会自动处理cookie,只要使用同一httpclient,且没有手动连接放掉httpclient

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class KeepSessionWithOneHttpclient {
	/**
	 * 如果用的是同一个HttpClient且没去手动连接放掉client.getConnectionManager().shutdown();
	 * 都不用去设置cookie的ClientPNames.COOKIE_POLICY。httpclient都是会保留cookie的
	 * @param loginUrl
	 * @param loginNameValuePair
	 * @param urlAndNamePairList
	 * @return
	 */
	public static Map<String,String> doPostWithOneHttpclient(String loginUrl,List<NameValuePair> loginNameValuePair,
			Map<String,List<NameValuePair>> urlAndNamePairList) {
		//返回每个URL对应的响应信息
		Map<String,String> map = new HashMap<String,String>();
		String retStr = "";//每次响应信息
		int statusCode = 0 ;//每次响应代码
		
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = null;
		closeableHttpClient = httpClientBuilder.build();
		HttpPost httpPost = new HttpPost(loginUrl);
		// 设置请求和传输超时时间
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(30000)
				.setConnectTimeout(30000).build();
		httpPost.setConfig(requestConfig);
		UrlEncodedFormEntity entity = null;
		try {
			if(null!=loginNameValuePair){
				entity = new UrlEncodedFormEntity(loginNameValuePair, "UTF-8");
				httpPost.setEntity(entity);
			}
			//登录
			CloseableHttpResponse loginResponse = closeableHttpClient.execute(httpPost);
			statusCode = loginResponse.getStatusLine().getStatusCode();
			retStr = EntityUtils.toString(loginResponse.getEntity(), "UTF-8");
			map.put(loginUrl, retStr);
			
			//登录后其他操作
			for(Map.Entry<String,List<NameValuePair>> entry : urlAndNamePairList.entrySet()){
				String url = entry.getKey();
				List<NameValuePair> params = urlAndNamePairList.get(url);
				httpPost = new HttpPost(url);
				if(null!=params){
					entity = new UrlEncodedFormEntity(params, "UTF-8");
					httpPost.setEntity(entity);
				}
				CloseableHttpResponse operationResponse = closeableHttpClient.execute(httpPost);
				statusCode = operationResponse.getStatusLine().getStatusCode();
				retStr = EntityUtils.toString(operationResponse.getEntity(), "UTF-8");
				map.put(url, retStr);
				
				if(statusCode == 302){
					String redirectUrl = operationResponse.getLastHeader("Location").getValue();
					httpPost = new HttpPost(redirectUrl);
					CloseableHttpResponse redirectResponse = closeableHttpClient.execute(httpPost);
					statusCode = redirectResponse.getStatusLine().getStatusCode();
					retStr = EntityUtils.toString(redirectResponse.getEntity(), "UTF-8");
					map.put(redirectUrl, retStr);
				}
			}
			// 释放资源
			closeableHttpClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	public static void main(String[] args) {
		//登录的地址以及登录操作参数
		String loginUrl = "http://public_ip/xxx/sso_login";
		//登录的相关参数以及登录后操作参数
		List<NameValuePair> loginParams = new ArrayList<NameValuePair>();
		loginParams.add(new BasicNameValuePair("sso_callback_uri","/xxx/forward?locale=zh_CN"));
		loginParams.add(new BasicNameValuePair("appName", "xxx"));
		loginParams.add(new BasicNameValuePair("username", "xxx"));
		loginParams.add(new BasicNameValuePair("password", "xxx"));
		
		//登录后操作地址以及登录后操作参数
		String queryUrl = "http://public_ip/xxx/system/getRegionList.do";
		List<NameValuePair> queryParams = new ArrayList<NameValuePair>();
		queryParams.add(new BasicNameValuePair("regionNo", "xxx"));
		queryParams.add(new BasicNameValuePair("pageNo", "xxx"));
		queryParams.add(new BasicNameValuePair("pageSize", "xxx"));

		Map<String,List<NameValuePair>> map = new HashMap<String,List<NameValuePair>>();
		map.put(queryUrl, queryParams);
		Map<String,String> returnMap = doPostWithOneHttpclient(loginUrl, loginParams, map);
		System.out.println(returnMap.toString());
	}
}

 

 4.第二种情况:登录一次,后续使用之前登录时的cookie

    登录后,将cookie保存起来,待有其他操作时,不用再次登录,首先设置cookie,然后调用操作

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class LoginWithHttpclient {
	static CookieStore cookieStore = null;

	/**
	 * 组装登录参数
	 * @return
	 */
	public static List<NameValuePair> getLoginNameValuePairList() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("sso_callback_uri","/xxx/forward?locale=zh_CN"));
		params.add(new BasicNameValuePair("appName", "xxx"));
		params.add(new BasicNameValuePair("username", "xxx"));
		params.add(new BasicNameValuePair("password", "xxx"));
		return params;
	}
    /**
	 * 组装操作参数
     * @return
     */
	public static List<NameValuePair> getQueryNameValuePairList() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("regionNo", "xxx"));
		params.add(new BasicNameValuePair("pageNo", "xxx"));
		params.add(new BasicNameValuePair("pageSize", "xxx"));
		return params;
	}
	/**
	 * 将cookie保存到静态变量中供后续调用
	 * @param httpResponse
	 */
	public static void setCookieStore(HttpResponse httpResponse) {
		System.out.println("----setCookieStore");
		cookieStore = new BasicCookieStore();
		// JSESSIONID
		String setCookie = httpResponse.getFirstHeader("Set-Cookie").getValue();
		String JSESSIONID = setCookie.substring("JSESSIONID=".length(),
				setCookie.indexOf(";"));
		System.out.println("JSESSIONID:" + JSESSIONID);
		// 新建一个Cookie
		BasicClientCookie cookie = new BasicClientCookie("JSESSIONID",JSESSIONID);
		cookie.setVersion(0);
		cookie.setDomain("domain");
		cookie.setPath("/xxx");
		cookieStore.addCookie(cookie);
	}

	public static String doPost(String postUrl,List<NameValuePair> parameterList) {
		String retStr = "";
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = null;
		if(cookieStore!=null){
			closeableHttpClient = httpClientBuilder.setDefaultCookieStore(cookieStore).build();
		}else{
			closeableHttpClient = httpClientBuilder.build();
		}
		HttpPost httpPost = new HttpPost(postUrl);
		// 设置请求和传输超时时间
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(30000).setConnectTimeout(30000).build();
		httpPost.setConfig(requestConfig);
		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameterList, "UTF-8");
			httpPost.setEntity(entity);
			CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
		    setCookieStore(response);

			HttpEntity httpEntity = response.getEntity();
			retStr = EntityUtils.toString(httpEntity, "UTF-8");
			System.out.println(retStr);
			// 释放资源
			closeableHttpClient.close();
		} catch (Exception e) {
		}
		return retStr;
	}

	public static void main(String[] args) {
		String loginUrl = "http://domain/xxx/sso_login";
		String queryReginUrl = "http://domani/xxx/system/getRegionList.do";
		//第一次登录会保存cookie
		doPost(loginUrl, getLoginNameValuePairList());
		//第二次操作会调用已经存在的cookie
		doPost(queryReginUrl, getQueryNameValuePairList());
	}
}

 

你可能感兴趣的:(httpclient妙用二 httpclient保持会话登录)