0
votes

I'm doing a project for electronic invoicing, which connects to a service using WSDL Soap next link Link Wsdl Service

So I use the WSDL Importer of Delphi to generate classes to connect.

My code to use the service

procedure TForm1.Button1Click(Sender: TObject);
var
  tBillServ : billService;
  tSendB    : sendBill;
  ByteA     : TByteDynArray;
  tSendBResp: sendBillResponse;
begin
  tSendB:=sendBill.Create;
  tSendBResp:=sendBillResponse.Create;
  tSendB.fileName:='Demo.zip';
  SetLength(ByteA,2);
  ByteA[0]:=10;
  ByteA[1]:=20;
  tSendB.contentFile:=ByteA;
  tSendBResp:=tBillServ.sendBill(tSendB);
end;

The data that I submit are just trial for a response from the server but me an error message appears, was looking for but could not find anything that can help me.

If a WS-Security connection is necessary to change the programming of the classes that are generated with the WSDL Importer?

I'm in big trouble, because they can not find a track to get to fix it.

I hope I can help, and sorry for my English, I use google translator

2
Out of the box, the delphi SOAP implementation does not support WS security. There is a unit floating around called wsse.pas, which you can find here.whosrdaddy

2 Answers

1
votes

I had the same problem last week. I'm using Delphi 2010.

First : I couldn't manage to implement a fully-functional TSOAPHeader descendant. The farthest I could reach was to either get the "wsse:" prefix in header nodes or making the wsse namespace properly declared. But they were mutually exclusive :(.

I could bet you got the following message when your tried to call your webservice method : "No WS-Security header". Right ?

The actual WORKING way to add a WS-Security header is to build the complete header node and add the following code in OnBeforeExecute() :

// Load a copy of the XML in a separate TXMLDocument
XMLDocument.LoadFromStream(SOAPRequest);<br> 
{ Build your complete proper WS-Security header node here)<br>
// Insert the header<br>
XMLDocument.DocumentElement.ChildNodes.Insert(0, SoapHeader);<br>
// THIS LINE IS MANDATORY, AS WE WISH TO OVERWITE THE REQUEST, NOT APPEND TO IT !!!<br>
SOAPRequest.Seek(0, soFromBeginning);<br>
// Overwrite the request<br>
XMLDocument.SaveToStream(SOAPRequest);

And then, free your XMLDocument, do the clean-up, etc... It should work.

0
votes

I resolved the problem by creating this feature

function GetbillService(UseWSDL: Boolean; Addr: string;var HTTPRIO: THTTPRIO): billService;
 const
  defWSDL = 'https://www.sunat.gob.pe:443/ol-ti-itcpgem-beta/billService?wsdl';
  defURL  = 'https://www.sunat.gob.pe:443/ol-ti-itcpgem-beta/billService';
  defSvc  = 'billService';
  defPrt  = 'BillServicePort';
 var
  RIO: THTTPRIO;
 begin
  if (Addr = '') then
  begin
    if UseWSDL then
      Addr := defWSDL
    else
      Addr := defURL;
  end;
  if HTTPRIO = nil then
    RIO := THTTPRIO.Create(nil)
  else
    RIO := HTTPRIO;
  try
    Result := (RIO as billService);
    if UseWSDL then
    begin
      RIO.WSDLLocation := Addr;
      RIO.Service := defSvc;
      RIO.Port := defPrt;
    end else
      RIO.URL := Addr;
  finally
    if (Result = nil) and (HTTPRIO = nil) then
      RIO.Free;
  end;

 end;

and I use it in this way

tBillServ:=GetbillService(True,'',HTTPRIO1);
tSendB:=sendBill.Create;
tSendBResp:=sendBillResponse.Create;
tSendB.fileName:='20100066603-01-F001-1.ZIP';
tSendB.contentFile:=FIleToByteArray('D:\E-Billing\20100066603-01-F001-1.ZIP');
tSendBResp:=tBillServ.sendBill(tSendB);

establish the connection but it appears that the security header is incorrect need to have the following characteristic

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body xmlns:NS1="http://service.sunat.gob.pe" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:NS2="urn:billService1"><NS1:sendBill><parameters href="#1"/></NS1:sendBill><NS2:sendBill id="1" xsi:type="NS2:sendBill"><fileName xsi:type="xsd:string">20100066603-01-F001-1.ZIP</fileName><contentFile xsi:type="xsd:base64Binary">UEsDBBQA.....DAwNBgAAAAAB
    AAEAawAAAD0MAAAAAA==</contentFile></NS2:sendBill></SOAP-ENV:Body></SOAP-ENV:Envelope>

but it appears aso

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Body>
        <sendBill xmlns="http://service.sunat.gob.pe">
            <parameters>
                <fileName>20100066603-01-F001-1.ZIP</fileName>
                <contentFile>UEsDBB.....NjY2MDMtA==</contentFile>
</parameters></sendBill></SOAP-ENV:Body></SOAP-ENV:Envelope>

I have tried to manually send the xml with the correct parameters but even so the security header error I get

procedure TForm1.HTTPRIO1BeforeExecute(const MethodName: string;
  SOAPRequest: TStream);
var
 Str:String;
 outs:TStream;
begin
  Str:= {Code XML};
  outs:=TStringStream.Create(Str);
  SOAPRequest:=outs;
end;

Review the information on the WSSE.pas where the UsernameToken is added but my implementation is not clear