有用过httpclient并顺利带验证码登陆的帮忙看下,一直成功不了快烦死了

有用过httpclient并成功带验证码登陆的帮忙看下,一直成功不了快烦死了
想做用httpclient做一个我们学校教务系统的代理登陆的APP,学校连接是http://jwgl2.jmu.edu.cn/Common/Login.aspx,通过抓包,发现要POST登陆信息的目标Url和登陆页面的Url是相同,并获得了要传的相关数据,先贴上最终POST登陆信息的代码
public void login() {
String target = "http://jwgl2.jmu.edu.cn/common/login.aspx"; // 要提交的目标地址 
HttpPost httpRequest = new HttpPost(target); // 创建HttpPost对象

// 将要传递的参数保存到List集合中
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("__EVENTARGUMENT",""));
params.add(new BasicNameValuePair("__EVENTTARGET",""));
params.add(new BasicNameValuePair("__LASTFOCUS","***"));// ***表示抓包抓的到值,代码太多,省略了
params.add(new BasicNameValuePair("__VIEWSTATE","***"));
params.add(new BasicNameValuePair("TxtUserName", username)); // 用户名
params.add(new BasicNameValuePair("TxtPassword", pwd)); // 密码
params.add(new BasicNameValuePair("TxtVeriCode",code));
params.add(new BasicNameValuePair("BtnLoginImage.x","1"));//这两个0表示登陆时候按了回车
params.add(new BasicNameValuePair("BtnLoginImage.y","1"));


try {
System.out.println("setHeader之前的cookie>>"+MainActivity.cookies.get(0).getValue());//测试cookie值是否相同

httpRequest.setHeader("Cookie","ASP.NET_SessionId="+MainActivity.cookies.get(0).getValue());
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置编码方式
HttpResponse httpResponse = MainActivity.httpclient
.execute(httpRequest); // 执行HttpClient请求
if (httpResponse.getStatusLine().getStatusCode() == 200) { 
// 如果请求成功
httpRequest.setHeader("Cookie", "ASP.NET_SeesionId=" +MainActivity.cookies.get(0).getValue());//得到最后POST完登陆信息后的cookie
System.out.println("最终post完后的cookie>>"+MainActivity.cookies.get(0).getValue());//测试用

/*有了cookie就可以尝试登陆成功之后的页面的打开了,下面的代码是打开一个课表作为测试*/
HttpGet httprs = new HttpGet("http://jwgl2.jmu.edu.cn/Student/ViewMyStuSchedule.aspx");
httprs.setHeader("Cookie","ASP.NET_SessionId="+MainActivity.cookies.get(0).getValue());//设置课表页面的cookie为刚刚获取的cookie,我测试过用IE登陆教务系统
//并抓到登陆成功的cookie,然后把抓到的value值直接加到setHeader上,可以访问课表页面,并讲html代码打出
HttpResponse httpResponse2 = new DefaultHttpClient().execute(httprs);
if(httpResponse2.getStatusLine().getStatusCode()==200){
StringBuffer sb = new StringBuffer();
HttpEntity entity = httpResponse2.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
//是读取要改编码的源,源的格式是GB2312的,安源格式读进来,然后再对源码转换成想要的编码就行
String data = "";
while ((data = br.readLine()) != null) {
sb.append(data);
}
result += sb.toString();  //此时result中就是我们成绩的HTML的源代码了
}
else{
result +="请求失败,返回值为:"+httpResponse2.getStatusLine().getStatusCode();//打印出返回码
}
} else {
result = "请求失败!!";
}  
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace(); // 输出异常信息
} catch (ClientProtocolException e) {
e.printStackTrace(); // 输出异常信息
} catch (IOException e) {
e.printStackTrace(); // 输出异常信息
}finally{
httpRequest.abort();
}

}

在这最后的POST之前是还要获取验证码的,由于要保证验证码的有效性,我是这样做的,在调用login()方法之前先执行一次post,并保存POST之后的cookie,然后带着这个cookie去访问验证码的页面。写在了一个handle里面
public void handleMessage(Message msg) {
if (result != null) {
Intent intent = getIntent(); // 获取Intent对象
Bundle bundle = new Bundle(); // 实例化传递的数据包


String target = "http://jwgl2.jmu.edu.cn/common/login.aspx"; // 要提交的目标地址
HttpPost httpRequest_1 = new HttpPost(target); // 创建HttpPost对象
// 将要传递的参数保存到List集合中
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("TxtUserName", username)); // 用户名
params.add(new BasicNameValuePair("TxtPassword", pwd)); // 密码
params.add(new BasicNameValuePair("BtnLoginImage.x","1"));//这两个0表示登陆时候按了回车
params.add(new BasicNameValuePair("BtnLoginImage.y","1"));
 

try {
httpRequest_1.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置编码方式
HttpResponse httpResponse1 = MainActivity.httpclient
.execute(httpRequest_1); // 执行HttpClient请求
if (httpResponse1.getStatusLine().getStatusCode() == 200) { 
// 如果请求成功


MainActivity.cookies=((AbstractHttpClient) MainActivity.httpclient).getCookieStore().getCookies();

System.out.println("第一次点击获取验证码时候的cookie>>"+MainActivity.cookies.get(0).getValue());
}
                                      /*这里是获取验证码,并在layout显示*/
checkicon = (ImageView) findViewById(R.id.code);
byte [] data = CheckCode.getImageViewArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
checkicon.setImageBitmap(bitmap);

}catch (UnsupportedEncodingException e1) {
e1.printStackTrace(); // 输出异常信息
} catch (ClientProtocolException e) {
e.printStackTrace(); // 输出异常信息
} catch (IOException e) {
e.printStackTrace(); // 输出异常信息
}finally{
httpRequest_1.abort();
}
}
super.handleMessage(msg);
}

最后附上验证码的获取方法,
public static byte[] getImageViewArray() {
byte[] data = null;
InputStream inputStream = null;
// 不需要关闭的输出流,直接写入到内存中
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet(URL_PATH);
HttpResponse httpResponse;
byte[] b_data = new byte[1024];
int len = 0;
try{
System.out.println(MainActivity.cookies.get(0).getValue());
httpRequest.setHeader("Cookie","ASP.NET_SessionId="+MainActivity.cookies.get(0).getValue());
httpResponse =httpclient.execute(httpRequest);

if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity entity = httpResponse.getEntity();
InputStream is = entity.getContent();
           while ((len = is.read(b_data)) != -1) {
        
            outputStream.write(b_data, 0, len);
          
      }
           
           data = outputStream.toByteArray();
           
          MainActivity.cookies2 = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();
          System.out.println("验证码的cookie:"+MainActivity.cookies2);
        //   MainActivity.cookies = ((AbstractHttpClient) MainActivity.httpclient).getCookieStore().getCookies();

}
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}

return data;
}
验证码的获取的时候我有setHeader("cookie","ASP.NET_SessionId="+MainActivity.cookies.get(0).getValue());这里的MainActiviy.cookies就是在上面那个handle里面第一次POST获取的,所以也应该能保证验证码的有效性了,但是在最后一次POST完学号,密码和获取的验证码之后,获取的cookie并不是认证成功的cookie,而且和第一次POST和获取验证码之后得到的Cookie值是不同的。导致最后的登陆是失败的,最后我也是一直卡在这里,希望有开发过httpclient带验证码登陆成功的前辈们给指点一下。。问题解决不了实在太难受了!!!
------解决思路----------------------
路过,帮顶。

这个过程是验证过的还是你猜的。建议用抓包工具把登录的全过程抓下来,然后参考Post的内容来用httpclient做Post。
------解决思路----------------------
mark一下
------解决思路----------------------
给个账号抓一下包
------解决思路----------------------
先解析登录界面的源代码,然后提取参数,并设置cookie,把参数的名值对发送给服务器,手工登陆一次,把登录成功之后的url拿下来,直接访问就可以了。刚刚做好这个程序。。。折腾了好久。。。。