HttpClient如何获得服务器端的SESSIONID并进行后续操作

用户在登录之后,需要保持登录状态,才能进行下一步操作,比如修改密码,获取个人信息等。

sessoin是用来在无状态的HTTP协议下越过多个请求页面来维持状态和识别用户。
服务器端在收到客户端传过来的请求会建立一个sessoin,而对于后续的请求,服务器首先需要确认该请求是否已经包含了一个可识别的sessoin标识,即sessoinid,如果已经包含,则将此session检索到继续使用,如果没有此标识,需要创建新的session。

在客户端,进行登录操作之后,如何保持登录状态继续后续操作。
登录获取sessionid并利用httpclient进行传递,如下代码。

public String getSessionId() {
		
		//创建默认实例,GET/POST对象代码省略
		
		//执行请求
		CloseableHttpResponse response = httpClient.execute(post, context);
		// 获取JSESSIONID
		cookie = context.getCookieStore().getCookies();   //可以将cookie打印查看里面的字段
		for (int i = 0; i < cookie.size(); i++) {
			if (cookie.get(i).getName().equals("JSESSIONID")) {
				SESSIONID = cookie.get(i).getValue();
				System.out.println("JSESSIONID--" + SESSIONID);
			}
		}
		return SESSIONID;
	}

第二部分,登录成功后想要获得用户个人信息,可以利用URL重定向,将sessionid加入隐藏字段,传给服务器。

// 显示用户个人信息的接口
	public Employee userInfoInterface(String SESSIONID) {

		Employee employee = null;
		CloseableHttpResponse response = null;
		String url = “xxxxxxxx”;
		// 创建默认的httpClient实例
		CloseableHttpClient httpClient = HttpClients.createDefault();
		// 创建GET对象
		HttpGet get = new HttpGet(
				url	+ ";jsessionid=" + SESSIONID); // URL重定向,sessionid字段名叫jsessionid
		System.out.println("url--" + SESSIONID);
		// 执行请求
		try {
			response = httpClient.execute(get);
			// 获取响应的结果
			HttpEntity entity = response.getEntity();
			String string = EntityUtils.toString(entity, "utf-8");
			System.out.println("滴滴滴滴--" + string);   //
			// JSON字符串的解析
			JSONObject object1 = JSONObject.fromObject(string);
			System.out.println("object1--" + object1);
			// 获取data的数据
			String info = object1.getString("data");
			JSONObject object2 = JSONObject.fromObject(info);
			// 得到个人信息
			// 时间的获取,string转换成date
			String dateString = object2.getString("regisdate").substring(0, 10); // 2017-07-07
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			try {
				date = dateFormat.parse(dateString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			employee = new Employee(object2.getString("username"),object2.getString("userpassword"),
					object2.getString("realname"), object2.getString("depart"),date,
					object2.getString("userlevel"),object2.getString("infolevel"),object2.getString("status"),
					object2.getString("lastalter"), object2.getString("email"));
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				// 关闭连接
				response.close();
				httpClient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return employee;
	}

而对于有参数需要通过post请求发送给服务器的,第一种可以在地址后直接加参数,或者构造参数列表添加,如

//第一种:
HttpPost post = new HttpPost(url+";jsessionid=" + SESSIONID 
						+ "?oldps=" + oldps + "&userpassword=" + userpwd
						);   //?和&符号不要忘记

//第二种
// 构造参数列表并添加
    List<NameValuePair> list = new ArrayList<NameValuePair>();
	list.add(new BasicNameValuePair("oldps", oldps));
	list.add(new BasicNameValuePair("userpassword", userpwd));
	UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(list,
					"UTF-8");
	post.setEntity(uefEntity);

以上,待完善。

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值