回 帖 发 新 帖 刷新版面

主题:【求助】JAVA客户端实现不了Session保持,帮忙看下是什么问题,或者有什么别的方法?

【求助】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();
            }
        }
    }
    
}

回复列表 (共4个回复)

沙发

up

各位大侠,帮帮忙啊!

板凳

期待答案!!!
期待高手!!!

3 楼

已经解决掉了。

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

多谢。

4 楼

我也在找

我来回复

您尚未登录,请登录后再回复。点此登录或注册