【发布时间】:2014-09-17 06:14:23
【问题描述】:
我在 Borland Delphi 7 中有一个使用 Indy 组件的程序,我想检查我的登录名和密码是否正确。
这是我当前的代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
IdSSL, IdSSLOpenSSL, IdCookieManager, StrUtils;
type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Button1: TButton;
Memo1: TMemo;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
IdCookieManager1: TIdCookieManager;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
post: TStringList;
poczatek, koniec: integer;
aStream: TMemoryStream;
HTML, authToken: string;
fLogin, fHaslo: string;
begin
IdHTTP1.HandleRedirects := True;
fLogin := Edit1.Text;
fHaslo := Edit2.Text;
HTML := IdHTTP1.Get('https://e-skok.pl/eskok/login.jsp');
poczatek := Pos('name="atlassian-token" value="', HTML) +
Length('name="atl_token" value="');
koniec := PosEx('"', HTML, poczatek);
authToken := copy(HTML, poczatek, koniec - poczatek);
post := TStringList.Create;
try
post.Add('atlassian-token=' + authToken);
post.Add('os_username=' + fLogin);
post.Add('os_password=' + fHaslo);
try
IdHTTP1.Request.ContentType:= 'application/json';
HTML:= IdHTTP1.Post('https://e-skok.pl/eskok/login.jsp', post);
Memo1.Text:= HTML;
Label1.Caption := 'YES';
except
if IdHTTP1.ResponseCode = 401 then
begin
ShowMessage('Nieprawidłowy login lub hasło.');
Label1.Caption := 'NO';
end
else
Label1.Caption := ':)';
end;
finally
post.Free;
end;
end;
end.
问题是当我尝试登录时,程序总是说“是”(这意味着它已连接,即使登录名和/或密码错误)。
通过 IdHTTP 检查与 jsp 站点的连接的正确方法是什么?
【问题讨论】:
-
HTML 和 IdHTTP1.ResponseCode 的值是多少?
-
HTML 的值是我要连接的 url 地址。未分配 IdHTTP1.ResposneCode 的值 - 程序应检查 ResponseCode 是否为 401 - 如果是,则程序应显示消息“登录名或密码错误”