Delphi Indy IdHTTP授权

时间:2016-05-19 07:36:27

标签: delphi indy idhttp

我尝试使用IdHTTPREST API

在Exchange中获取用户照片

How to: Get user photos by using EWS in Exchange

procedure TsMain.IdHTTP1Authorization(Sender: TObject;
  Authentication: TIdAuthentication; var Handled: Boolean);
begin
  Authentication.Username := DomainName +'\'+ User;
  Authentication.Username := User + #64 + DomainName;
  Authentication.Password := Password;
  Handled:=true;
end;

function TsMain.GetUserPhoto(const eMail: string):Boolean;
var
  s: TStream;
  ResponseCode: Integer;
begin
  Result := False;
  idHTTP:= TIdHttp.Create(Application);
  s := TFileStream.Create('mypic.jpg', fmCreate);
  try
    idHTTP.OnAuthorization := IdHTTP1Authorization;
    idHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(idHTTP);
    idHTTP.HandleRedirects := True;
    idHTTP.Request.BasicAuthentication:= true;
    idHTTP.Get('https://ServerName/ews/Exchange.asmx/s/GetUserPhoto?email='+eMail+'&size=HR240x240', s);
    ResponseCode := idHTTP.Response.ResponseCode;
    case ResponseCode of
      200:
        begin
          Result := True;
        end;
    else
       ShowMessage(idHTTP.Response.ResponseText);
    end;
  finally
    s.Free;
    idHTTP.Free;
  end;
end;

我得到HTTP/1.1 401 Anonymous Request Disallowed

为什么idHTTP未向请求发送身份验证?我做错了什么?

0 个答案:

没有答案