正方教务系统验证码自动识别

  • 2016-09-12
  • 0
  • 0

将此工程导入myeclipse

ImagePreProcess.srcPath = ctrackcode_srcimg;
    ImagePreProcess.trainPath = ctrackcode_trainimg;
    String name = request.getParameter("c").toString();      //给图片加个缓存名称
        String checkcodeUrl = request.getParameter("url").toString();  //此图片的地址
 
    String checkCodeImg="a";
    String secretcode = "";
            try{
                 checkCodeImg = ImagePreProcess.downloadImage(checkcodeUrl+"?", name+".png");                
            }catch(Exception e){
            }
            if (checkCodeImg != "a" ||checkCodeImg != null ) {
                    secretcode = ImagePreProcess.getAllOcr(checkCodeImg);
                    File ml =new File(checkCodeImg);
                    ml.delete();
                    out.print(secretcode);
            }

测试地址:
http://115.28.41.123:8080/WhxyJw/yzm.jsp?c=312fz&url=http://218.56.144.42/CheckCode.aspx
若非正方教务平台的请改这里的值:

—-非正方区—-

修改那两个值

—-非正方结束—-

由于众所周知,验证码需要加入cookie去获取

所以我单独写了一个php的请求验证码:

$url = "http://218.56.144.42/CheckCode.aspx";  
$curl = curl_init();
if($_POST){
$cookie=$_POST['c'];
}else{
    $cookie=$_GET['c'];
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl);
curl_close($curl);
$filename=substr($cookie,-10);
$a="./yzm/2".$filename.".png";  //将其保存为图片放在服务器
$fp = fopen($a,"wb");
fwrite($fp, $data);
fclose($fp);

然后用保存在服务器的图片,用PHP请求识别:

//大众性
$login_url="http://115.28.41.123:8080/WhxyJw/yzm.jsp";
$l=$login_url."?c={$filename}&url=http://gades.ikay.me/vcode/{$filename}.jpg";
//部分PHP传值会出现问题则用下面的
/*
$login_url="http://115.28.41.123:8080/WhxyJw/yzm.jsp";
$fileurl="http://gades.ikay.me/vcode/{$filename}.jpg";
$fileurl=urlencode($fileurl);
$l=$login_url."?c={$filename}&url={$fileurl}";
*/
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL,$l);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            $yyy=curl_exec($ch);
        curl_close($ch);
        echo substr($yyy,-4);

整合出来:

$url = "http://218.56.144.42/CheckCode.aspx";  
$curl = curl_init();
if($_POST){
$cookie=$_POST['c'];
}else{
    $cookie=$_GET['c'];
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($curl);
curl_close($curl);
$filename=substr($cookie,-10);
$a="./yzm/2".$filename.".jpg";
$fp = fopen($a,"wb");
fwrite($fp, $data);
fclose($fp);
 
$login_url="http://115.28.41.123:8080/WhxyJw/yzm.jsp";
$l=$login_url."?c={$filename}&url=http://gades.ikay.me/vcode/{$filename}.jpg"; 
$ch=curl_init(); 
curl_setopt($ch, CURLOPT_URL,$l); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$yyy=curl_exec($ch); 
curl_close($ch); 
echo substr($yyy,-4); 
unlink($a); //删除服务器保存的验证码图片

返回的直接是识别出来的验证码。

评论

还没有任何评论,你来说两句吧

CC共享许可协议:BY-NC-SA 4.0 - 友情链接 - Theme by Qzhai