使用THTTPRIO时,如何停止显示身份验证对话框

时间:2015-02-23 12:44:47

标签: delphi wininet delphi-xe7 idhttp

我使用以下代码使用基本身份验证连接到Web服务:

var
  RIO: THTTPRIO;
begin
  RIO := THTTPRIO.Create(nil);

  EndPoint := GetWebServicePort(True, '', RIO);

  RIO.HTTPWebNode.UserName := 'xxxx';
  RIO.HTTPWebNode.Password := 'yyyy';
...
end;

如果用户名和密码正确,一切正常。但是,如果它们不正确,则会弹出一个Windows对话框,请求正确的凭据。我需要捕捉错误而不是对话框。

enter image description here

如何停止弹出对话框?我搜索过并找到了几个结果(Link 1Link 2),但似乎都没有提供真正的解决方案。

1 个答案:

答案 0 :(得分:1)

要捕获错误,您可以使用HTTP客户端库(例如Indy TIdHTTP)首先在Web服务地址上运行HTTP GET(或HEAD)请求,并捕获在用户/密码为时引发的异常错。

uses
  ... IdHTTP ...;

...
var
  HTTP: TIdHTTP;

ValidCredentials := False;
...    
HTTP.Request.Username := username;
HTTP.Request.Password := password;
HTTP.Request.BasicAuthentication := True;
try
  HTTP.Head(url);
  ValidCredentials := HTTP.ResponseCode = 200;
except
  on ... (some Indy exception) do
  begin
    // signal that username / password are incorrect
    ...
  end;
end;

if ValidCredentials then
begin
  // invoke Web Service ...