3

我想在 Delphi 7 中使用 Indy 10 Components 进行 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 代理授权错误。有人可以指导我做错了什么吗?

提前致谢

4

1 回答 1

0

尝试在您的 IdHTTP1SelectProxyAuthorization 方法中省略以下代码行:

idHTTP1.ProxyParams.Clear;

在我看来,这会清除代理的所有设置,这就是您收到错误的原因。

于 2013-04-26T08:25:52.587 回答