package com.zzsn.utility;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.util.Date;

public class test {

	public static void main(String[] args) throws Exception{
		//时间戳
		long timestamp = new Date().getTime();
		//请求地址
		String url = "https://auth.xhszjs.com/uias/initLogin.do?appCode=silkroad_znsj?_=" + timestamp;
		HttpClient client = new HttpClient();
		//post请求方式
		PostMethod postMethod = new PostMethod(url);
		//推荐的数据存储方式,类似key-value形式
		NameValuePair telPair = new NameValuePair();
		telPair.setName("loginname");
		telPair.setValue("z_dianxin_zhangqiankun");
		NameValuePair pwdPair = new NameValuePair("j_password","zqk@9988");
		//封装请求参数
		postMethod.setRequestBody(new NameValuePair[]{telPair,pwdPair});
		//这里是设置请求内容为json格式,根据站点的格式决定
		//因为这个网站会将账号密码转为json格式,所以需要这一步
		postMethod.setRequestHeader("Content_Type","application/x-www-form-urlencoded");
		//执行请求
		client.executeMethod(postMethod);
		//通过Post/GetMethod对象获取响应头信息
		String cookie = postMethod.getResponseHeader("Set-Cookie").getValue();
		//截取需要的内容
//		String sub = cookie.substring(cookie.indexOf("&"), cookie.lastIndexOf("&"));
//		String[] splitPwd = sub.split("=");
//		String pwd = splitPwd[1];
		System.out.println(cookie);
	}

}

