步骤
1.打开网站http://esales.10010.com/,将jessionId,cookie,验证码,,viewState保存下来
2.模拟登录,设置cookie等信息
3.返回登录的结果,发现跟抓包工具获取的信息不一致,即使登录错误也没提示
1).设置jsessionId,cookie等为全局变量
 if (StringUtils.isBlank(LoginMes.getSessionId())) {// 如果JsessionId为空,需要重取
String jsessionId = "0GmrRDpHphl76H1Ty1ZTjQSs27vvXw1TRsHpNBYLNGy2C7bQHsSq!1861677778";
Cookie[] cookies = new Cookie[2];
cookies[0] = new Cookie("esales.10010.com", "JSESSIONID", jsessionId);
cookies[1] = new Cookie(".10010.com", "LOGINVERIFYCODE", "8XlcQT7RNvs=");
LoginMes.setCookies(cookies);
Map<String,String> map = new HashMap<String, String>();
map.put("jsessionid", "JSESSIONID="+jsessionId);
map.put("loginVerifycode", "8XlcQT7RNvs=");
map.put("verifyUrl", Constant.PICNAME);
LoginMes.setLoginMap(map);
}
2).登录的方法
 /** 得到客户端
 * @param path
 * @param nameValuePairs
 * @param encode
 * @param sessionId
 * @return
 */
public static HttpClient getHttpClient(String path, NameValuePair[] nameValuePairs, String encode, String sessionId) {
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(path);
postMethod.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
postMethod.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
postMethod.setRequestHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
postMethod.setRequestHeader("Connection", "keep-alive");
    postMethod.setRequestHeader("Cookie", "");
postMethod.setRequestHeader("Host", "esales.10010.com");
postMethod.setRequestHeader("Referer", "esales.10010.com");
postMethod.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
postMethod.setRequestHeader("P3P", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
postMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
if (nameValuePairs != null) {
postMethod.setRequestBody(nameValuePairs);
}
try {
client.getParams().setParameter("http.protocol.single-cookie-header", true);
int stats = client.executeMethod(postMethod);
client.getState().clearCookies();
client.getState().addCookie(new Cookie(".10010.com", "LOGINVERIFYCODE", "8XlcQT7RNvs="));
client.getState().addCookie(new Cookie("esales.10010.com", "JSESSIONID", sessionId.substring("JSESSIONID=".length())));
if (stats == HttpStatus.SC_OK) {
// 登陆成功后需要设置对应的cookie
System.out.println("联网成功!");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
postMethod.releaseConnection();// 这里最好把之前的资源放掉
//CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
return client;
}
3.获取viewState,该值已存在文件d:\zt.log中
public static String getViewState() {
String fullcanshu = "";
try {
File file = new File("d:/zt.log");
FileInputStream inputStream = new FileInputStream(file);
BufferedReader reader = new  BufferedReader(new InputStreamReader(inputStream,"utf-8"));
String canshu = null;
while((canshu = reader.readLine())!=null){
fullcanshu += canshu;
}
reader.close();
inputStream.close();
} catch (Exception e) {

}
return fullcanshu;
}
4.判断是否登陆成功,根据响应ContentLength判断
public static boolean isLogin() {
boolean login = false;// 登陆未成功
String jsessionid = LoginMes.getSessionId();
if (StringUtils.isNotBlank(jsessionid)) {
try {
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams,5000); //设置连接超时为5秒
        org.apache.http.client.HttpClient client = new DefaultHttpClient(httpParams); // 生成一个http客户端发送请求对象
      //在本地上下问中绑定一个本地存储
//创建一个本地上下文信息
        HttpPost httpPost = new HttpPost(Constant.HTTPURL+";"+jsessionid); //设定请求方式
        httpPost.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
        httpPost.getParams().setParameter("http.protocol.single-cookie-header", true);
        HttpResponse httpResponse = client.execute(httpPost); // 发送请求并等待响应
          // 判断网络连接是否成功
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
         System.out.println("网络错误异常!!!!");
            }
        HttpEntity entity = httpResponse.getEntity(); // 获取响应里面的内容
        if (-1 != entity.getContentLength()) {
         login = true;
        }
        Header[] headers =  httpResponse.getAllHeaders();
        for (Header header : headers) {
         System.out.println("----headerName:" +header.getName() +"value:" + header.getValue());
        }
        System.out.println("clietCOOkie" + client.getParams().getParameter("Cookie"));
        /*// 得到服务气端发回的响应的内容(都在一个字符串里面)
        InputStream inputStream = entity.getContent();  // 得到服务气端发回的响应的内容(都在一个流里面)
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
String line = "";
while((line = reader.readLine())!=null){
if(line.indexOf("form")!=-1){
login = false;
break;
}
}*/
      } catch (Exception e) {
       login = false;
       System.out.println("这是异常!");
      }
} else {
login = false;
System.out.println("已超时,请重新登陆!");
}
return login;
}
5.登陆
setJessionId();
NameValuePair[] nameValuePairs = { new NameValuePair("AJAXREQUEST","_viewRoot")
,new NameValuePair("_authKey","")
,new NameValuePair("j_id2:province","17")
,new NameValuePair("tokenPwd","")
,new NameValuePair("username","sdsc-yaosz3")// 用户名
,new NameValuePair("password","YSZ2013")// 密码
,new NameValuePair("verifyCode","rzun")// 验证码
,new NameValuePair("j_id2_SUBMIT","1")
,new NameValuePair("j_id2:j_id6","j_id2:j_id6")
,new NameValuePair("javax.faces.ViewState",getViewState())};
String path = Constant.HTTPURL;
HttpClient client = getHttpClient(path, nameValuePairs,  "utf-8", LoginMes.getSessionId()); 
Cookie[] cookies = client.getState().getCookies();
System.out.println("登陆的cookie");
String cookieStr = "";
for (Cookie cookie : cookies) {
cookieStr += cookie.getName()+ "=" + cookie.getValue() + ";";
}
System.out.println(cookieStr);
LoginMes.setCookies(client.getState().getCookies());
boolean s = isLogin();
if (s) {
System.out.println("已登陆");
} else {
System.out.println("未登陆");
}