With frmMain.idhtp1 Do
Begin
request.CustomHeaders.Add('Cookie:' + Cookie);
ShowMessage(Cookie);
end;设置我是这么设置的、通过POST方法提交后、通过弹出的提示框我也确认cookies没有问题、可是呢、网站提示不正确、我通过抓包发现
POST /index.php/account/loginSave HTTP/1.0
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 72
Cookie: PHPSESSID=8s4e1thsvh0dtsn92cqlguvom5
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
这里的Cookie: PHPSESSID=8s4e1thsvh0dtsn92cqlguvom5的值并不是我所设置的、那么我想问下、是我设置的方法错误呢、还是有其他什么原因呢?
有下面的函数获得验证码
Function TfrmMain.GetYzm:string;
Var
Html:string;
PicMemoryStream:TMemoryStream;
Jpg:TJPEGImage;
i:Integer;
Begin//这是获得验证码的函数
PicMemoryStream:=TMemoryStream.Create;
Jpg:=TJPEGImage.Create;
Html:=idhtp1.Get('http://www.19mayi.com/index.php/account/login');//这里访问登陆界面
Html:=Copy(Html,Pos(UpperCase('http://www.19mayi.com//captcha/'),UpperCase(Html)),Length('http://www.19mayi.com//captcha/1276754021.3704.jpg'));
//frmLogin.MyCookies:=idhtp1.CookieManager.CookieCollection.Cookie
PicMemoryStream:=TMemoryStream.Create;
Jpg:=TJPEGImage.Create;
//wb1.Navigate('about:blank');
idhtp1.Get(Html,PicMemoryStream);
Try
wb1.Navigate(Html);
While wb1.Busy Do
Application.ProcessMessages;
Except
GetYzm;
Exit;
End;
frmCookies:=(wb1.document as ihtmldocument2).cookie;
frmLogin.MyCookies:=frmCookies;
ShowMessage('获得的'+frmCookies);
PicMemoryStream.Position:=0;
Jpg.LoadFromStream(PicMemoryStream);
frmLogin.img1.Picture.Assign(Jpg);
Jpg.Free;
PicMemoryStream.Free;
{frmLogin.MyCookies:='';
For i:= 0 To idhtp1.CookieManager.CookieCollection.Count - 1 Do
Begin
frmLogin.MyCookies:=frmLogin.MyCookies + idhtp1.CookieManager.CookieCollection.Items[i].CookieText;
end;
frmLogin.MyCookies:=}
//wb1.Navigate(Html);//这里就是访问验证码的地址咯?
Result:=Copy(Html,32,15);
end;存在下面的函数提交登陆信息
Function TfrmMain.LoginWeb(Const User,Pwd,YZM,Sid,Cookie:string):Boolean;
Var
S,Tmp:TStringStream;
Html:string;
Begin//这是提交信息的函数
Tmp:=TStringStream.Create('');
ShowMessage('提交的:'+Cookie);
With frmMain.idhtp1 Do
Begin
s:=TStringStream.Create('uname=' + User + '&password=' + Pwd + '&seccode_time'+ Sid +'=&seccode='+ YZM);
HandleRedirects:=True;
Request.ContentType:='application/x-www-form-urlencoded';
Request.AcceptLanguage:=' zh-cn';
Request.Referer:='http://www.19mayi.com/index.php/account/login';
Request.Accept:='image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*';
Request.Connection:='Keep-Alive';
request.Referer:='http://www.19mayi.com/index.php/account/login';
request.CustomHeaders.Add('Cookie:' + Cookie);
Request.AcceptEncoding:='gzip, deflate';
Request.UserAgent:=' Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
Request.Host:='www.19mayi.com';
Request.ContentLength:=Length('uname=' + User + '&password=' + Pwd + '&seccode_time'+ Sid +'=&seccode='+ YZM); Post('http://www.19mayi.com/index.php/account/loginSave',s,tmp);//这里提交数据
Html:=UTF8Decode(Tmp.DataString);
If Pos(UpperCase('<p class="flash err">'),UpperCase(Html)) > 0 Then
Begin
Result:=False;
Html:=UpperCase(
Copy(Tmp.DataString,Pos(UpperCase('<p class="flash err">'),UpperCase(Html)),Length(Html))
);
Html:=LeftStr(Html,Pos('</P>',Html) - Length('</P>'));
ShowMessage(Html);
Application.MessageBox('登陆失败、请确认用户名、密码、验证码正确后、再次尝试、、',
'提示', MB_OK + MB_ICONINFORMATION);
End
Else
Begin
Result:=True;
ShowMessage(Html);
WriteHtml(Html,wb1);
End;
end;
//WriteHtml(Tmp.DataString,wb1);
s.Free;
Tmp.Free;
end;
可是呢、根据服务器的返回值、显示总是说验证码错误、希望有人可以帮助我、、
先idhttp.Get('http://www.19mayi.com/index.php/account/login');获取cookie,然后添加cookie到idhttp,用此idhttp获取图片,最后用此idhttp.post(),你在except里递归GetYzm,要是别人上不了网,还不死循环啊。
我按照6F的说吧、先clear然后add、结果依旧是一样的、、
希望大家能够给于一点帮助、或者可以帮助我测试下看、这个问题已经困扰我较长时间了、始终没有得到解决、、
request.RawHeaders.values['Cookie'] :=cookie;