日期:2014-05-20  浏览次数:20675 次

httpclient 4.3 通过验证码登录总是失败,请高手指教!
String verifyImageUrl = "http://localhost:8080/verifyCode.aspx";
String loginUrl = "http://localhost:8080/login.aspx";
byte[] htmlVerifyImage = WebUtil.getByteByUrl(httpClient, verifyImageUrl);
ImageIcon img = new ImageIcon(htmlVerifyImage);
//保存图片,图片内容是1-9,a-z,A-Z的一个字符窜
String verifyCode = "xxxxxx";//图片内容
//下面进行登录操作,输入用户名/密码/验证码(图片内容)
HttpPost httpPost = new HttpPost(loginUrl);
httpPost.setHeader("User-Agent","Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0"); httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPost.setHeader("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
List<NameValuePair> nvp = new ArrayList<NameValuePair>();

    nvp.add(new BasicNameValuePair("username", "admin"));

    nvp.add(new BasicNameValuePair("password", "admin"));

    nvp.add(new BasicNameValuePair("verifycode",verifyCode));

   HttpResponse httpResponse = null;
   try {
httpPost.setEntity(new UrlEncodedFormEntity(nvp, HTTP.UTF_8));
httpResponse = httpClient.execute(httpPost);
System.out.println(httpResponse.getStatusLine().getStatusCode());
HttpEntity httpEntity = httpResponse.getEntity();

后面就不写了,反正又回到登录页面;直接用浏览器就会跳到首页,表示登录成功

大侠们帮忙看看,问题出在哪?


------解决方案--------------------
当前验证码和你获取的验证码不一致吧,又跳到首页。
------解决方案--------------------
这些类似的如sesseion 验证码 ,cookie 等方面看看。
------解决方案--------------------
没看到你去判断验证码的代码阿,单这些看不出来问题
------解决方案--------------------
验证码没有匹配对