我想在Delphi 7中使用Indy 10组件进行NTLM身份验证。 这是我的源代码:
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs ,StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP,IdAuthenticationSSPI , IdAuthentication , IdAuthenticationDigest , IdHeaderList;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.add( IdHTTP1.Get('http://www.google.co.in'));
end;
procedure TForm1.IdHTTP1ProxyAuthorization(Sender: TObject;
Authentication: TIdAuthentication; var Handled: Boolean);
var
UserName: string;
UserNameLen: Dword;
begin
Authentication.Username := 'xxxxx';
Authentication.Password := 'xxxx';
Handled := False; // Tried setting it to True , No Changes in error
End;
procedure TForm1.IdHTTP1SelectProxyAuthorization(Sender: TObject;
var AuthenticationClass: TIdAuthenticationClass;
AuthInfo: TIdHeaderList);
begin
// First check for NTLM authentication, as you do not need to
// set username and password because Indy will automatically
// handle passing your Windows Domain username and
// password to the proxy server
if (pos('Proxy-Authenticate: NTLM', idHTTP1.Response.RawHeaders.Text)>0) then
begin
idHTTP1.ProxyParams.Clear;
idHTTP1.ProxyParams.BasicAuthentication := false;
// Set the authentication class to NTLM
//idhttp1.auth
AuthenticationClass := TIdSSPINTLMAuthentication;
end
else
begin
// Next check for Basic
if (pos('Proxy-Authenticate: Basic', idHTTP1.Response.RawHeaders.Text)>0) then
begin
AuthenticationClass := TIdBasicAuthentication;
idHTTP1.ProxyParams.BasicAuthentication := true;
end
else
begin
// Then Digest
if (pos('Proxy-Authenticate: Digest', idHTTP1.Response.RawHeaders.Text)>0) then
AuthenticationClass := TIdDigestAuthentication
end;
end;
idHTTP1.ProxyParams.ProxyUsername := 'xxxx';
idHTTP1.ProxyParams.ProxyPassword := 'xxxx';
end;
根据我在互联网上阅读的内容,我还将IdHttp.pas的源代码修改为: -
function TIdCustomHTTP.DoOnProxyAuthorization(ARequest: TIdHTTPRequest; AResponse: TIdHTTPResponse): Boolean;
var
i: Integer;
S: string;
Auth: TIdAuthenticationClass;
begin
Inc(FAuthProxyRetries);
if not Assigned(ProxyParams.Authentication) then
begin
// Find which Authentication method is supported from us.
i := 0;
while i < AResponse.ProxyAuthenticate.Count do
begin
S := AResponse.ProxyAuthenticate[i];
try
Auth := FindAuthClass(Fetch(S));
break;
except
end;
inc(i);
end;
if i = AResponse.ProxyAuthenticate.Count then
begin
result := false;
exit;
end;
if Assigned(FOnSelectProxyAuthorization) then
begin
OnSelectProxyAuthorization(self, Auth, AResponse.ProxyAuthenticate);
end;
{ ProxyParams.Authentication := Auth.Create;
end;
result := Assigned(OnProxyAuthorization);
// Clear password and reset autorization if previous failed
if (AResponse.FResponseCode = 407) then begin
ProxyParams.ProxyPassword := '';
ProxyParams.Authentication.Reset;
end; }
/// *** Changes Start **** ////
if Assigned(Auth) then
begin
ProxyParams.Authentication := Auth.Create;
end;
end;
result := Assigned(ProxyParams.Authentication) and Assigned(OnProxyAuthorization);
// Clear password and reset autorization if previous failed
if ((AResponse.FResponseCode = 407) and
(not (ProxyParams.Authentication is TIdSSPINTLMAuthentication))) then
begin
ProxyParams.ProxyPassword := '';
if Assigned(ProxyParams.Authentication) then
begin
ProxyParams.Authentication.Reset;
end;
End;
//// **** Changes end *** /////
if Result then
begin
with ProxyParams.Authentication do
begin
Username := ProxyParams.ProxyUsername;
Password := ProxyParams.ProxyPassword;
AuthParams := AResponse.ProxyAuthenticate;
end;
result := false;
repeat
case ProxyParams.Authentication.Next of
wnAskTheProgram: // Ask the user porgram to supply us with authorization information
begin
if Assigned(OnProxyAuthorization) then
begin
ProxyParams.Authentication.Username := ProxyParams.ProxyUsername;
ProxyParams.Authentication.Password := ProxyParams.ProxyPassword;
OnProxyAuthorization(self, ProxyParams.Authentication, result);
if result then begin
ProxyParams.ProxyUsername := ProxyParams.Authentication.Username;
ProxyParams.ProxyPassword := ProxyParams.Authentication.Password;
end
else begin
break;
end;
end;
end;
wnDoRequest:
begin
result := true;
break;
end;
wnFail:
begin
result := False;
Break;
end;
end;
until false;
// *** Changes start *** ///
If Result then
begin
Response.KeepAlive := ProxyParams.Authentication.KeepAlive;
end;
///*** Changes End *** ///
端; 端;
我仍然收到407代理授权错误。有人可以指导我做错了吗?
提前致谢
答案 0 :(得分:0)
尝试在IdHTTP1SelectProxyAuthorization方法中省略以下代码行:
idHTTP1.ProxyParams.Clear;
在我看来,这会清除代理的所有设置,这就是您收到错误的原因。