首先,对不起我的英语不好!
我需要在网站 www.nfe.fazenda.org.br 上进行查询。为了获得最佳性能,我将组件 TIdHTTP 与 TIdCookieManager 一起使用。
本网站使用验证码来控制访问。所以,我正在尝试获取页面和验证码以获取 cookie。
用户输入 NFe 的验证码和密钥。所以,我用帖子发送到页面。
但是,当我运行帖子时,我被重定向到错误页面。
这是我的测试代码,请您帮助我。谢谢!
unit Forms.MainForm;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Forms, Vcl.Graphics, Vcl.Dialogs, Vcl.Controls, Vcl.ExtCtrls,
Vcl.StdCtrls,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL,
IdCookieManager, IdCookie, IdURI,
GIFImg, WinInet;
type
TMainForm = class(TForm)
mem: TMemo;
IdHttp: TIdHTTP;
IdSSLHandlerSocket: TIdSSLIOHandlerSocketOpenSSL;
IdCookieManager: TIdCookieManager;
panBottom: TPanel;
btnGo: TButton;
imgCaptcha: TImage;
edtKey: TEdit;
edtCode: TEdit;
lblInit: TLabel;
procedure FormShow(Sender: TObject);
procedure lblInitClick(Sender: TObject);
procedure btnGoClick(Sender: TObject);
private
Cookies: TIdCookies;
viewState, eventValidate: string;
procedure GetHiddenFieldValues(html: string);
procedure p_Execute;
end;
var
MainForm: TMainForm;
const
HOST = 'http://www.nfe.fazenda.gov.br';
URLIMG = 'http://www.nfe.fazenda.gov.br/scripts/srf/intercepta/captcha.aspx?opt=image';
URLGET = 'http://www.nfe.fazenda.gov.br/portal/consulta.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8=';
URLPOST = 'http://www.nfe.fazenda.gov.br/portal/consultaCompleta.aspx?tipoConteudo=XbSeqxE8pl8=';
CONTENT_TYPE = 'application/x-www-form-urlencoded';
implementation
{$R *.dfm}
procedure TMainForm.FormShow(Sender: TObject);
begin
lblInitClick(Sender);
end;
procedure TMainForm.lblInitClick(Sender: TObject);
var
response: TMemoryStream;
gif: TGIFImage;
html: string;
begin
response := TMemoryStream.Create;
gif := TGIFImage.Create;
try
html := IdHttp.Get(URLGET);
mem.Text := html;
GetHiddenFieldValues(html);
IdHttp.Get(URLIMG, response);
response.Position := 0;
gif.LoadFromStream(response);
imgCaptcha.Picture.Assign(gif);
Cookies := IdCookieManager.CookieCollection;
finally
gif.Free;
response.Free;
end;
end;
procedure TMainForm.btnGoClick(Sender: TObject);
begin
p_Execute;
end;
procedure TMainForm.GetHiddenFieldValues(html: string);
var
nIni, nLen: integer;
cVal: string;
const
TAG_VIEWSTATE = '<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="';
TAG_EVENTVALIDATION = '<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="';
begin
nIni := Pos(TAG_VIEWSTATE, html);
nLen := Length(TAG_VIEWSTATE);
cVal := Copy(html,nIni+nLen, Length(html));
cVal := Copy(cVal, 1, Pos('" />', cVal)-1);
viewState := cVal;
nIni := Pos(TAG_EVENTVALIDATION, html);
nLen := Length(TAG_EVENTVALIDATION);
cVal := Copy(html,nIni+nLen, Length(html));
cVal := Copy(cVal, 1, Pos('" />', cVal)-1);
eventValidate := cVal;
end;
procedure TMainForm.p_Execute;
var
params: TStringList;
Uri: TIdURI;
nI: Integer;
begin
params := TStringList.Create;
Uri := TIdURI.Create(Cookies[0].Domain);
try
for nI := 0 to Pred(Cookies.Count) do
begin
IdCookieManager.AddServerCookie(Cookies[nI].ClientCookie, Uri);
if nI = 0 then
IdHttp.Request.CustomHeaders.Values['Cookie'] := Cookies[nI].ClientCookie
else
IdHttp.Request.CustomHeaders.Values['Cookie'] := IdHttp.Request.CustomHeaders.Values['Cookie'] + '; ' + Cookies[nI].ClientCookie;
end;
params.Add('__VIEWSTATE=' + viewState);
params.Add('__EVENTVALIDATION=' + eventValidate);
params.Add('__EVENTTARGET=');
params.Add('__EVENTARGUMENT=');
params.Add('ctl00$txtPalavraChave=');
params.Add('ctl00$ContentPlaceHolder1$txtChaveAcessoCompleta=' + edtKey.Text);
params.Add('ctl00$ContentPlaceHolder1$txtCaptcha=' + edtCode.Text);
params.Add('ctl00$ContentPlaceHolder1$btnConsultar=Continuar');
params.Add('hiddenInputToUpdateATBuffer_CommonToolkitScripts=1');
IdHttp.Request.ContentType := CONTENT_TYPE;
mem.Text := IdHttp.Post(URLPOST, params);
finally
params.Free;
Uri.Free;
end;
end;
end.