首页 > 解决方案 > Delphi TidHTTP web 服务安全用户名token

问题描述

我正在尝试使用 Delphi 和 IndyTIdHTTP访问使用安全用户名令牌方面的站点。

SOAP 请求具有密码类型=urladdressforoasis 密码的标头

来自供应商的服务器classnameproxy.config文件有:

<basicHttpBinding>
    <binding name="BasicHttpBinding_ZZ_myInt">
        <security mode="TransportWithMessageCredential" />
    </binding>
</basicHttpBinding>

所以我试图添加自定义标题以TIdHTTP使用以下代码:

idHTTP.Request.CustomHeaders.AddValue('Authorization', 'Basic ' + TIdEncoderMIME.EncodeString(string(aUserID) + ':' + string(aPWD), IndyTextEncoding_ASCII()));

显然,由于某种原因,这是不正确的。我四处寻找TIdHTTPoasis 和 usernametoken ,但一无所获。也许我正在寻找错误的关键字?

是否可以TIdHTTP使用安全用户名令牌访问 Web 服务?

function Post_XML(aURL, aUserID, aPWD, aInputFile, aOutputFile, aContentType, aCharset, aShow: pAnsiChar):integer; stdcall;
var
  sRequest,
  sResponse : TFileStream;
  idHTTP  : TidHTTP;
  SSLHandler : TidSSLIOHandlerSocketOpenSSL;
  EnvVar  : string;
begin
  Result := 0;
  begin
    try
    begin
      sRequest := TFileStream.Create(string(aInputFile), fmOpenRead or fmShareDenyWrite);
      sResponse := TFileStream.Create(string(aOutputFile), fmCreate or fmShareDenyRead or fmOpenWrite);
    end;
    except on E: Exception do
    begin
      Result := 99;
    end;
    end;
    try
    begin
      EnvVar := '';
      EnvVar := System.SysUtils.GetEnvironmentVariable('use_a_proxy');
      SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
      SSLHandler.SSLOptions.SSLVersions := [sslvSSLv23, sslvSSLv3, sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
      SSLHandler.SSLOptions.Mode := sslmUnassigned;
      idHTTP := TidHTTP.Create;
      idHTTP.ConnectTimeout := 60000;
      idHTTP.ReadTimeout := 60000;
      idHTTP.HandleRedirects := True;
      idHTTP.IOHandler := SSLHandler;
      if UpperCase(EnvVar) = 'ON' then
      begin
        idHTTP.ProxyParams.ProxyServer := '127.0.0.1';
        idHTTP.ProxyParams.ProxyPort := 8888;
      end;
      idHTTP.Request.ContentType := string(aContentType);
      idHTTP.Request.CharSet := string(aCharSet);
      idhttp.Request.CustomHeaders.AddValue('SOAPAction', 'http://bustax.tntax.tn.gov/BusTaxRequest');
      if aUserid <> '' then
      begin
        idHTTP.Request.BasicAuthentication := True;
        idhttp.Request.Username := aUserid;
        idHTTP.Request.Password := aPwd;
      end;
      idHTTP.Post(trim(string(aURL)), sRequest, sResponse);
      FreeAndNil(SSLHandler);
      FreeAndNil(idHTTP);
    end;
    except
      on E: EIdHTTPProtocolException do
      begin
        ShowMessage(AnsiString('Protocol Error: ' + E.Message));
        Result := E.ErrorCode;
       end;
      on E: EIdSocketError  do
      begin
        ShowMessage(AnsiString('Socket Error: ' + E.Message));
        Result := E.LastError;
      end;
      on E: EIdException  do
      begin
        ShowMessage(AnsiString('HTTP Error: ' + E.Message));
        Result := HTTPMain.ResponseCode;
      end;
      on E: Exception  do
      begin
        ShowMessage(AnsiString('Error: ' + E.Message));
        Result := HTTPMain.ResponseCode;
      end;
    end;
    FreeAndNil(sRequest);
    FreeAndNil(sResponse);
    end;
end;
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:bus="http://bustax.tntax.tn.gov">
<soapenv:Header>
<oas:Security>
<oas:UsernameToken>
<oas:Username>myUsername</oas:Username>
<oas:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">myPassword</oas:Password>
</oas:UsernameToken>
</oas:Security>
</soapenv:Header>
<soapenv:Body>
<bus:BusTaxRequest>
<bus:JurisdictionSITUS>1900</bus:JurisdictionSITUS>
<bus:TransactionId>123456</bus:TransactionId>
</bus:BusTaxRequest>
</soapenv:Body>
</soapenv:Envelope>

标签: delphiindyidhttpusernametoken

解决方案


推荐阅读