【求助】JAVA客户端实现不了Session保持,帮忙看下是什么问题,或者有什么别的方法?实现功能:
自动登录有密码,图片验证码验证的网站逻辑描述:
1:用HttpClient首先通过Get方法取得图片验证码的输出流,
2:将图片验证码输出流解析得到验证码,
3:用Post方法将用户名,密码和验证码提交给服务器。
   (因为一般验证码验证要求Session信息,
     这里会将第一步取验证码时返回的Session信息附带到发出的Header中)
4:根据服务器的输出判断是否验证成功。
现在已经实现到100%解析验证码正确,并且将数据提交到了服务器(第三步),
但是服务器的输出却是【验证码错误】,
我已经经过VB(WebViewer)测试,确认过只要Session不变,
将上一次取出的验证码提交给服务器的话,服务器是会通过验证的。
请问为什么用JAVA,服务器却会认为验证码出错呢?
附代码求各位高手解惑!谢谢!
不胜感激!================================================================================
    public static void fncSubmit (
                        String inXXXXCode
                        , String inInputId
                        , String inPassword) {
        HttpClient httpClient = null;
        PostMethod postMethod = null;
        int statusCode = 0;
        String strResponseBody = "";
        try {
            httpClient = new HttpClient();
            httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);            // 通过GET方法取得验证码
            GetMethod get = new GetMethod("/genImage/getImg.do");
            get.setRequestHeader("Accept", "*/*");
            get.setRequestHeader("Referer", "LoginAction.do");
            get.setRequestHeader("Accept-Language", "zh-cn");
            get.setRequestHeader("UA-CPU", "x86");
            get.setRequestHeader("Accept-Encoding", "gzip, deflate");
            get.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
            get.setRequestHeader("Host", "xxx.xxx.com");
            get.setRequestHeader("Connection", "Keep-Alive");
            get.setRequestHeader("Cookie", "JSESSIONID="
                    + strSessionid + "; XXXX=xxxx; XXXX=x; XXXX=");            statusCode = httpClient.executeMethod(get);            // 对GET方法的输出进行分析,获取验证码
            Gather gGather = new Gather();
            String strSecurityCode = gGather.
            getSecurityCodeNoCache(get.getResponseBodyAsStream());
            System.out.println("The Security Code is:" + strSecurityCode);
            //get.releaseConnection();
            
            // 保存Session信息
            if (strSessionid.trim().equals("")) {
                if (statusCode == HttpStatus.SC_OK) {
                    try {
                        Cookie[] cookies = httpClient.getState().getCookies();
                        if (cookies != null) {
                            if (cookies[0] != null) {
                                strSessionid = cookies[0].getValue();
                            }
                        }
                    } catch (Exception ex) {
                        System.out.println(ex.toString());
                    }
                }                httpClient = null;
            }            
            // 通过POST方法将用户名,密码,取得的验证码提交给服务器
            httpClient = new HttpClient();
            httpClient.getHostConfiguration().setHost("xxx.xxx.com", 80, "https");
            httpClient.getParams().setContentCharset("GB2312");
            postMethod = new PostMethod(ConstantUtil.strSubmitPreLoginUrl);
            
            postMethod.setRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            postMethod.setRequestHeader("Referer", "LoginAction.do");
            postMethod.setRequestHeader("Accept-Language", "zh-cn");
            postMethod.setRequestHeader("ContentType", "application/x-www-form-urlencoded");
            postMethod.setRequestHeader("UA-CPU", "x86");
            postMethod.setRequestHeader("Accept-Encoding", "gzip, deflate");
            postMethod.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
            postMethod.setRequestHeader("Host", "xxx.xxx.com");
            postMethod.setRequestHeader("Content-Length", "136");
            postMethod.setRequestHeader("Connection", "Keep-Alive");
            postMethod.setRequestHeader("Cache-Control", "no-cache");
            postMethod.setRequestHeader("Cookie", "JSESSIONID="
                    + strSessionid + "; XXXX=xxxx; XXXX=x; XXXX=");            NameValuePair[] data = {
                    new NameValuePair("method", "login"),
                    new NameValuePair("xxx", "1"),
                    new NameValuePair("xxx", "xxx"),
                    new NameValuePair("xxx", "xxx"),
                    new NameValuePair("xxx", "xxx"),
                    new NameValuePair("xxx", "xxx"),
                    new NameValuePair("xxx", "x"),
                    new NameValuePair("inputid", inInputId),
                    new NameValuePair("password", inPassword),
                    new NameValuePair("Code", strSecurityCode)};
            
            postMethod.setRequestBody(data);
            statusCode = httpClient.executeMethod(postMethod);
            
            if (statusCode == HttpStatus.SC_OK) {
                strResponseBody = postMethod.getResponseBodyAsString();
                System.out.println("The page html is:" + strResponseBody);
                
                if (strResponseBody.indexOf("验证码错误") != -1) {
                    System.out.println("The Security Code Is Not Correct");
                }
            }
            
            postMethod.releaseConnection();
        } catch (HttpException ex) {
            if (postMethod != null) {
                postMethod.releaseConnection();
            }
        } catch (IOException ex) {
            if (postMethod != null) {
                postMethod.releaseConnection();
            }
        } catch (Exception ex) {
            if (postMethod != null) {
                postMethod.releaseConnection();
            }
        }
    }
    
}

解决方案 »

  1.   

    Session是在服务器端的,在客户端肯定是不行的在客户端一般用cookie保存数据
      

  2.   

    谢谢楼上回答,client确实不能保持session,
    不过http返回的header中可以得到上一次的session内容,
    这样在http请求的header中加上上一次session内容就可以实现模拟IE那样的session保持了。
    也就是模拟cookie实现session保持。
      

  3.   

    已经解决掉了。原因可能是因为HTTPClient封装了一些特殊的处理,
    如果用JAVA自带的HTTPURLConnection就没有问题了。多谢。