因工作需要,模拟登录一平台,提交数据。但一直没有成功。
具体如下:
先模拟登录首页,后模拟获取验证码,再模拟验证,cookie已设置,但模拟登录时错误,抓取的网页如下,程序代码再下面。麻烦高手给指导下。
谢谢!模拟登录时时抓取的网页:
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Set-Cookie: name=value; HttpOnly
Set-Cookie: SPRING_SECURITY_REMEMBER_ME_COOKIE=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/IOS
Location: http://*.*.*.*/login.do;jsessionid=hJzj8g+uD5MpziVfAFqoGbof.undefined?error=true
Content-Length: 0
Date: Sun, 24 Jan 2016 02:29:01 GMT
php 码:<?php
header('Content-Type:text/html;charset=utf-8');
date_default_timezone_set('PRC');$cookie_file= tempnam('./data','cookie');$urla='http://******/login.do';
$aurl= curl_init();
$timeout=10;
curl_setopt($aurl,CURLOPT_URL, $urla);
curl_setopt($aurl,CURLOPT_HEADER,1);
curl_setopt($aurl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($aurl,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($aurl,CURLOPT_COOKIESESSION,true);
curl_setopt($aurl,CURLOPT_COOKIEJAR, $cookie_file);
$contents=curl_exec($aurl);
file_put_contents('login.txt', $contents);
curl_close($aurl);//获取验证码图片内容,手工写入文件code.txt
$urlb='http://******/Kaptcha.jpg';//echo "正在取验证码";
$burl=curl_init();
curl_setopt($burl,CURLOPT_URL, $urlb);
curl_setopt($burl,CURLOPT_HEADER, 0);
curl_setopt($burl,CURLOPT_RETURNTRANSFER,1);
//curl_setopt($curl,CURLOPT_POSTFIELDS,$post_login);
curl_setopt($burl,CURLOPT_COOKIEFILE, $cookie_file);
$img=curl_exec($burl);
$fp=fopen("verifyCode.jpg","w");
fwrite($fp,$img);
fclose($fp);
curl_close($burl);//验证进入系统 ,验证码取值于code.txt文件,通过获取的img图片手工输入
sleep(50);$code=file_get_contents("code.txt");
$urld='http://*******8/j_spring_security_check';
$j_random=$code;
$post_login='j_type=username&j_username=***&j_password=****&j_random='.$j_random.'';$durl=curl_init();
curl_setopt($durl,CURLOPT_URL, $urld);
curl_setopt($durl,CURLOPT_HEADER,1);
curl_setopt($durl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($durl,CURLOPT_POST, 1);
curl_setopt($durl,CURLOPT_POSTFIELDS,$post_login);
curl_setopt($durl,CURLOPT_COOKIEFILE, $cookie_file);
$conn=curl_exec($durl);
file_put_contents('post_login.txt', $conn);
curl_close($durl);//登录成功后再做一些操作,提交内容等等,代码略unlink($cookie_file);  ?>