bestdqf
bestdqf
2011-06-29 09:14

如何模拟登录带有验证码的网站 小弟在此先谢谢了 给个思路就可以

已采纳

我要完成功能是模拟登录久游网
https://passport.9you.com/login.php
说明:
1. 提交的地址是https://login.passport.9you.com/identifyingCode.jsp 这是一个https的地址 牵扯到一些ssl,证书之类的东西. 但是jdk已经集成了的证书,所以就不需要考虑这些,可以当做http来处理.(这是我现在的理解,也可能不对)
2. 验证码是用户来输入的,暂时不考虑破解问题
3. 我用HttpAnalyzerStdV5(这是一个http截包工具,下载地址http://idc218.newhua.com/down/HttpAnalyzerFullV5_Trial.zip)对一次请求过程截包:
发现需要提交图中的这些数据 userName,password,identityingCode.就是用户名,密码,验证码,
id,continue,userIP,s都是隐藏的数据可以从源文件中看的到,
但是最后的x和y的值看不到,我测试了几次发现他是鼠标在登陆按钮那个图片的坐标
我猜现在登陆不成功就是这里的问题,但是这两个值在form中和js中查看不到
4. 测试账号jiuyoumoni 密码111111
5. 我的这这样做的
a) 根据 https://login.passport.9you.com/identifyingCode.jsp 下载图片并记录他的cookie 这个值要填入下一个请求的地址中
b) 组织以下数据并提交的https://login.passport.9you.com/checkCode

这个时候X和Y的值无法确定,也不能随便填一个
我的QQ19739257 可以随时联系我

以下的可执行代码,运行的时候在c盘下面建立文件夹CheckCode 验证码放到这个文件夹里
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;

public class tempClass {

public static void Login() {
    try {
        // 下载验证码到本地
        URL url = new URL("https://login.passport.9you.com/identifyingCode.jsp");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(10000);
        ((HttpURLConnection) connection).setRequestMethod("POST");
        connection.setRequestProperty("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
        connection.connect();

        String cookies = connection.getHeaderField("Set-Cookie");
        if (cookies.indexOf(";") != -1) {
            cookies = cookies.substring(0, cookies.indexOf(";"));
        }


        File imgCodeFile = new File("c:\\CheckCode\\"
                + System.currentTimeMillis() + ".gif");
        BufferedImage image = ImageIO.read(connection.getInputStream());
        ImageIO.write(image, "gif", imgCodeFile);

        String checkCode = GetCheckCode();
        System.out.println(checkCode);







        // 登录
        //隐藏参数
        String id = "SSO_PAY";
        String s = "5384a672b08ac3e96ad534ac67e30442";

        String userIp = "60.191.73.11";//这个是我的ip地址 到时候根据你自己的ip来填写 可以从form里查看值 

        String nextUrl = "http://pay.9you.com/funpay/checkstat.php";
        nextUrl = URLEncoder.encode(nextUrl, "GBK");

        //用户要输入值
        String userName = "jiuyoumoni";
        String password = "111111";
        String identifyingCode = checkCode;


        //生成地址https://login.passport.9you.com/checkCode?id=SSO_PAY&s=5384a672b08ac3e96ad534ac67e30442&userIp=60.191.73.11&userName=zhao88zhao8&password=458458&identifyingCode=5pre
        String paramStr = "?" + "id=" + id + "&s=" + s + "&userIp=" + userIp + "&userName=" + userName + "&password=" + password + "&identifyingCode=" + identifyingCode;



        String loginUrl = "https://login.passport.9you.com/checkCode" + paramStr;

        System.out.println("请求地址:" + loginUrl);



        //根据
        url = new URL("https://login.passport.9you.com/checkCode");
        connection = (HttpsURLConnection) url.openConnection();
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setConnectTimeout(20000);
        connection.setReadTimeout(20000);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", String.valueOf(paramStr.length()));
        connection.setRequestProperty("Cookie", cookies);
        connection.setRequestProperty("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
        connection.connect();
        DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
        dos.writeBytes(paramStr);
        dos.flush();
        dos.close();
        int res = connection.getResponseCode();

        if (res == 200) {
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "GBK"));
            String retVal;
            while ((retVal = in.readLine()) != null) {
                System.out.println(retVal);
            }
        }

    } catch (Exception e) {
        System.out.println("code error");
    }

}

private static String GetCheckCode() {
    Scanner sc = new Scanner(System.in);
    System.out.print("验证码在C:\\ImageCode目录下 ,请你查看并输入:");
    String checkCode = sc.next();
    System.out.println("您输入的验证码是:" + checkCode);
    return checkCode;
}

public static void main(String[] args) {
    tempClass.Login();
}

}

以下是可执行代码

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • magicghost magicghost 10年前

    import java.awt.image.BufferedImage;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Scanner;

    import javax.imageio.ImageIO;

    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.cookie.CookiePolicy;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;

    public class SimulateLogin {

    public static void login() {   
        HttpClient client = new HttpClient();//相当于客户端浏览器   
        //client.getHostConfiguration().setHost("https://login.passport.9you.com", 80);//比如www.oschina.net 80是端口   
        client.getParams().setCookiePolicy( 
                CookiePolicy.BROWSER_COMPATIBILITY); 
        try {   
            PostMethod post = new PostMethod("https://login.passport.9you.com/identifyingCode.jsp");// 验证码的相对路径   
            int statusCode = client.executeMethod(post);   
            //post.releaseConnection();//释放连接   
            if(HttpStatus.SC_OK==statusCode){//状态码200表示请求成功,先发送去请求验证码   
                InputStream is = post.getResponseBodyAsStream();   
                BufferedImage image = ImageIO.read(is);   
                File imgCodeFile = new File("c:\\CheckCode\\" 
                        + System.currentTimeMillis() + ".gif"); 
                ImageIO.write(image, "gif", imgCodeFile); 
                Scanner sc = new Scanner(System.in); 
                System.out.print("验证码在C:\\ImageCode目录下 ,请你查看并输入:"); 
                String inputCode = sc.next(); 
    
                String id = "SSO_PAY"; 
                String s = "5384a672b08ac3e96ad534ac67e30442"; 
                String userIp = "122.235.252.74";
                //用户要输入值 
                String userName = "jiuyoumoni"; 
                String password = "111111"; 
                String identifyingCode = inputCode; 
                //生成地址https://login.passport.9you.com/checkCode?id=SSO_PAY&s=5384a672b08ac3e96ad534ac67e30442&userIp=60.191.73.11&userName=zhao88zhao8&password=458458&identifyingCode=5pre 
                String paramStr = "?" + "id=" + id + "&s=" + s + "&userIp=" + userIp + "&userName=" + userName + "&password=" + password + "&identifyingCode=" + identifyingCode; 
                String loginUrl = "https://login.passport.9you.com/checkCode" + paramStr; 
                System.out.println("请求地址:" + loginUrl); 
    
                PostMethod post1 = new PostMethod(loginUrl);//登陆或注册的提交路径   
                statusCode = client.executeMethod(post1);   
                //post1.releaseConnection();//释放连接   
                if(HttpStatus.SC_OK==statusCode){//状态码200表示请求成功,先发送去请求验证码   
                    System.out.println(post1.getResponseBodyAsString());//简单查看一下打印信息有没有成功的信息   
                } else if (HttpStatus.SC_MOVED_TEMPORARILY == statusCode) {
                    //从头中取出转向的地址
                    Header locationHeader = post1.getResponseHeader("location");
                    String location = null;
                    if (locationHeader != null) {
                        location = locationHeader.getValue();
                        System.out.println("请求重定向至:" + location);
                        GetMethod redirect=new GetMethod(location);
                        statusCode = client.executeMethod(redirect);
                        if(HttpStatus.SC_OK==statusCode){
                            BufferedReader in = new BufferedReader(new InputStreamReader( 
                                    redirect.getResponseBodyAsStream(), "GBK")); 
                            StringBuffer sb = new StringBuffer();
                            String line = "";
    
                            while ((line = in.readLine()) != null) { 
                                sb.append(line);
                            } 
                            System.out.println("重定向页面:" + sb.toString()); 
                        }
                    } else {
                        System.err.println("Location field value is null.");
                    }
                }
            }
        } catch (HttpException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }  
    }
    
    public static void main(String[] args) { 
        login(); 
    } 
    

    }

    点赞 评论 复制链接分享

相关推荐