【千里码】Task34-喜刷刷

来源:互联网 发布:淘宝网pc版登录网址 编辑:程序博客网 时间:2024/04/29 12:05

计算一个验证码,他是一个自然数x,能使 md5(当天日期+你的用户名+当前的票数+x)的前6位都是0
举例: 假如2015年12月4号食年已经拿到了1014票,当你想投第1015票的时候,你的验证码可以是12011618,因为 “20151204shinian101412011618”的md5值是”0000003A19CF73CF3E9799219A9FFF4F”,这个md5前6位都是0

MD5加密算法

/**     * MD5加密     *      **/    public static String getMd5(String str){        StringBuffer buf = new StringBuffer("");        int i;        try{         MessageDigest md5=MessageDigest.getInstance("MD5");         md5.update(str.getBytes());         byte b[] = md5.digest();         for (int offset = 0; offset < b.length; offset++) {             i = b[offset];             if (i < 0)                 i += 256;             if (i < 16)                 buf.append("0");             buf.append(Integer.toHexString(i));         }        }         catch(NoSuchAlgorithmException e){             e.printStackTrace();         }        return buf.toString();    }

发送get请求的方法

/**     * Get方式请求url     **/     public static void sendGet(String url, String param) {            BufferedReader in = null;            try {                String urlNameString = url + "?" + param;                URL realUrl = new URL(urlNameString);                // 打开和URL之间的连接                URLConnection connection = realUrl.openConnection();                // 设置通用的请求属性                connection.setRequestProperty("accept", "*/*");                connection.setRequestProperty("connection", "Keep-Alive");                connection.setRequestProperty("user-agent",                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");                // 建立实际的连接                connection.connect();                // 定义 BufferedReader输入流来读取URL的响应                in = new BufferedReader(new InputStreamReader(                        connection.getInputStream()));            } catch (Exception e) {                System.out.println("发送GET请求出现异常!" + e);                e.printStackTrace();            }            // 使用finally块来关闭输入流            finally {                try {                    if (in != null) {                        in.close();                    }                } catch (Exception e2) {                    e2.printStackTrace();                }            }     }

发送验证码的方法

/**     * 获取验证码     */    public static void main(String args[]){        //获取当前日期        Date now = new Date();         //设置日期转换格式        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");        //个人昵称        String name="your name";        //请求地址        String url ="http://www.qlcoder.com/train/handsomerank";        //token值        String token="your token";        //用于保存拼接完的参数值        String param="";        String str = df.format(now)+name;        String answer ="";        //MD5加密后的值        String strMd5="";        for(int i=1;i<1000;i++){            for(long j=0;;j++){                //日期+昵称+票数+验证码                answer=str+i+j;                strMd5 = getMd5(answer);                //比较验证码前6位                if (strMd5.startsWith("000000")) {                    System.out.println(j);                    System.out.println("票数"+i);                    //拼接参数值                    param = "_token="+token+"&user="+name+"&checkcode="+j;                    //请求url                    sendGet(url,param);                    break;                }            }        }    }

经试验这程序的速度简直不忍直视,但就我现在的水平而言,除了增加线程,无法做出明显的优化,因此代码的优化就只能靠各位大神

0 0
原创粉丝点击