Search This Blog

Tuesday, July 10, 2018

Oracle 11g -> SOAP Webservice consume (client) from plsql

DECLARE
  v_value        VARCHAR2(4000);
  soap_request   clob;-- can use varchar2 also but its limit is 32767
  http_req       UTL_HTTP.REQ;
  http_resp      UTL_HTTP.RESP;
  t_buffer       VARCHAR2(32767);
  t_http_version VARCHAR2(20)   := 'HTTP/1.1';
  t_content_type VARCHAR2(60)   := 'text/xml; charset=utf-8';
  t_url          VARCHAR2(1000) := 'http://test.com/TestEmpStatistics'; --- update this accordingly without '?wsdl'
  l_msg varchar2(2000);
  --l_soap_req clob;
  l_refno varchar2(20);

BEGIN

BEGIN
--SELECT XML_DOC INTO SOAP_REQUEST FROM XML_DOCUMENT;
   --xx_localempstatistics_p ('C', soap_request, l_msg, l_refno);--create procedure to give out as xml document,
   --check my blog how to create xml output, procedure name ' P_XML_TRANSACTION_DOCUMENT2'
--link : http://mogalafzal.blogspot.com/2018/04/apply-absence-sshr-transaction-and.html
   soap_request := '
<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Header>
...
</soap:Header>

<soap:Body>
...
  <soap:Fault>
  ...
  </soap:Fault>
</soap:Body>

</soap:Envelope> ';
   DBMS_OUTPUT.PUT_LINE(l_refno);
   exception when others then
   dbms_output.put_line('exception under procedure begin');
END;
--soap_request := ;
  http_req := UTL_HTTP.BEGIN_REQUEST(t_url ,'POST' ,t_http_version);
  UTL_HTTP.SET_HEADER(http_req, 'User-Agent', 'Mozilla/4.0');
  UTL_HTTP.SET_HEADER(http_req, 'Content-Type', t_content_type);
  UTL_HTTP.SET_HEADER(http_req, 'Content-Length', lengthb(soap_request)); --lengthb for multilingual
  utl_http.set_header(http_req, 'SOAPAction', 'PushTestEmpStatistics');
  UTL_HTTP.WRITE_TEXT(http_req, soap_request);
  http_resp := UTL_HTTP.GET_RESPONSE(http_req);

  DBMS_OUTPUT.PUT_LINE('Response Status: ' ||http_resp.status_code||' ' || http_resp.reason_phrase);
  BEGIN
    LOOP
      UTL_HTTP.READ_TEXT(http_resp, t_buffer);
      DBMS_OUTPUT.PUT_LINE(substr(t_buffer, 1, 32767));
   END LOOP;
   EXCEPTION
    WHEN UTL_HTTP.END_OF_BODY THEN
    DBMS_OUTPUT.PUT_LINE('under exception loop');
      UTL_HTTP.END_RESPONSE(http_resp);
  END;
--  ELSE
--  DBMS_OUTPUT.PUT_LINE('NOT EXECUTED, UNDER ELSE');
--  END IF;

  EXCEPTION WHEN OTHERS THEN
  DBMS_OUTPUT.PUT_LINE('under final exception');
  DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
  DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;

No comments:

Post a Comment