3

我使用 InternetErrorDlg 请求登录名/密码(它是 delphi,但我认为它很清楚)

function ShowLoginDlg(Request: HINTERNET): boolean;
var
  DlgError: Cardinal;
begin
  DlgError := InternetErrorDlg(GetDesktopWindow, Request, 0,
    FLAGS_ERROR_UI_FILTER_FOR_ERRORS or
    FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS or
    FLAGS_ERROR_UI_FLAGS_GENERATE_DATA, PPointer(nil)^{like NULL in C/C++});
  if DlgError = ERROR_CANCELLED then Abort; // Abort raises exception
  if (DlgError <> ERROR_SUCCESS) and (DlgError <> ERROR_INTERNET_FORCE_RETRY) then
    RaiseLastOSError(DlgError); // RaiseLastOSError raises exception
  Result := DlgError = ERROR_INTERNET_FORCE_RETRY;
end;

while True do
begin
  SendRequest(Request);
  if not ((GetStatusCode(Request) = 401) and ShowLoginDlg(Request)) do
    Continue;
  CheckStatusCode(Request); // raises exception if status code >= 400
end;

并且有一个问题。即使用户勾选“保存密码”,密码并没有真正保存。所以,当我再次关闭并打开我的程序时,网络资源返回 401 给我,我必须再次显示登录对话框。

有任何想法吗??

如果我按照 RRUZ 的建议添加 FLAGS_ERROR_UI_SERIALIZE_DIALOGS,我不会得到任何效果。我的回调没有被调用并且密码没有被保存。

function InternetAuthNotifyCallback(
      dwContext: DWORD; // as passed to InternetErrorDlg
      dwReturn: DWORD; // error code: success, resend, or cancel
      lpReserved: Pointer // reserved: will be set to null
      ): DWORD; stdcall; // stdcall, is it right?
begin
  Result := 0; // What do I have to do??
end;

  type
    PFN_AUTH_NOTIFY = function(
      dwContext:DWORD;
      dwReturn:DWORD;
      lpReserved:Pointer): DWORD; stdcall;
    PINTERNET_AUTH_NOTIFY_DATA = ^INTERNET_AUTH_NOTIFY_DATA;
    INTERNET_AUTH_NOTIFY_DATA = packed record
      cbStruct: DWORD;
      dwOptions: DWORD;
      pfnNotify: PFN_AUTH_NOTIFY;
      dwContext: PDWORD;
    end;

  function ShowLoginDlg(Request: HINTERNET): boolean;
  var
    DlgError: Cardinal;
    PData: Pointer;
    Data: INTERNET_AUTH_NOTIFY_DATA;
  begin
    ZeroMemory(@Data, SizeOf(Data));
    Data.cbStruct := SizeOf(Data);
    Data.pfnNotify := InternetAuthNotifyCallback;
    PData := @Data; // if I set PData := Pointer(1) there is **not** AV!!!!
          // InternetErrorDlg don't use this parameter???
    DlgError := InternetErrorDlg(Application.MainForm.Handle, Request, 0,
      FLAGS_ERROR_UI_FILTER_FOR_ERRORS or
      FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS or
      FLAGS_ERROR_UI_FLAGS_GENERATE_DATA or
      FLAGS_ERROR_UI_SERIALIZE_DIALOGS,
      PData); // really InternetErrorDlg get pointer to PData, because in Delphi this param is passed by ref
    if DlgError = ERROR_CANCELLED then Abort;
    if (DlgError <> ERROR_SUCCESS) and (DlgError <> ERROR_INTERNET_FORCE_RETRY) then
      RaiseLastOSError(DlgError);
    Result := DlgError = ERROR_INTERNET_FORCE_RETRY;
  end;
4

1 回答 1

3

根据有关InternetErrorDlg函数的文档,您必须在方法调用中包含 FLAGS_ERROR_UI_SERIALIZE_DIALOGS标志。

FLAGS_ERROR_UI_SERIALIZE_DIALOGS

为密码缓存条目上的并发请求序列化身份验证对话框。lppvData 参数应该包含一个指向 INTERNET_AUTH_NOTIFY_DATA 结构的指针的地址,并且客户端应该实现一个线程安全的、非阻塞的回调函数。

于 2012-12-12T18:37:57.790 回答