Thanks Connor.
Could you please have a look at my below anonymous block where I am stuck while trying to convert binary data (not BASE64Binary but actually binary characters) back to original format (PDF file).
--------------------------------------------------------
/* Formatted on 3/10/2017 10:31:37 AM (QP5 v5.149.1003.31008) */
DECLARE
soap_request VARCHAR2 (30000);
soap_respond CLOB; -- varchar2(30000);
http_req UTL_HTTP.req;
http_resp UTL_HTTP.resp;
l_resp XMLTYPE;
l_blob BLOB;
l_header VARCHAR2 (32000);
l_raw LONG RAW;
buffer CLOB;
l_CLOB CLOB;
final_blob BLOB;
BEGIN
soap_request :=
'<soap:Envelope xmlns:soap="
http://www.w3.org/2003/05/soap-envelope" xmlns:grad="
http://grades.web.chemspec.quality.allvac.com/" >
<soap:Header/>
<soap:Body>
<grad:getGradeInfoForRM>
<strRMItem>104PT</strRMItem>
</grad:getGradeInfoForRM>
</soap:Body>
</soap:Envelope>';
http_req :=
UTL_HTTP.
begin_request ('
http://159.59.1.134:80/chemspec/CSGradeWSPort', 'POST',
'HTTP/1.1');
sys.UTL_HTTP.
SET_HEADER (http_req,
'Content-Type',
'application/soap+xml;charset=UTF-8');
UTL_HTTP.set_header (http_req, 'Content-Length', LENGTH (soap_request));
UTL_HTTP.set_header (http_req, 'Transfer-Encoding', 'chunked');
sys.UTL_HTTP.
set_header (http_req, 'User-Agent', 'Apache-HttpClient/4.1.1 (java 1.5)');
UTL_HTTP.set_header (http_req, 'SOAPAction', '');
UTL_HTTP.write_text (http_req, soap_request);
http_resp := UTL_HTTP.get_response (http_req);
DBMS_OUTPUT.put_line ('got the soap response');
DBMS_OUTPUT.
put_line ('Response> status_code: "' || http_resp.status_code || '"');
DBMS_OUTPUT.
put_line ('Response> reason_phrase: "' || http_resp.reason_phrase || '"');
DBMS_OUTPUT.put_line ('before read_text');
--
-- read ascii until you get to a key phrase which means the binary is
-- to follow.
--
BEGIN
LOOP
UTL_HTTP.read_text (http_resp, soap_respond);
EXIT WHEN REGEXP_INSTR (soap_respond, '[^[:print:]]') > 0; --l_header = 'binary data'; - -Not working
--l_header := SUBSTR(soap_respond, 1,INSTR(soap_respond,'%PDF') );
END LOOP;
--CONVERT ABOVE l_header usign XMLTYPE Later
DBMS_OUTPUT.put_line (' l_header: ' || l_header);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Error: ' || SQLERRM);
END;
--
-- then start on the binary stream
--
buffer := SUBSTR (soap_respond, INSTR (soap_respond, '%PDF'));
DBMS_OUTPUT.put_line ('Buffer: ' || buffer);
DBMS_LOB.createtemporary (l_blob, TRUE);
BEGIN
LOOP
UTL_HTTP.read_raw (http_resp, l_raw, 32767);
IF l_raw IS NOT NULL AND LENGTH (l_raw) > 0
THEN
DBMS_LOB.writeappend (l_blob, UTL_RAW.LENGTH (l_raw), l_raw);
END IF;
END LOOP;
EXCEPTION
WHEN UTL_HTTP.end_of_body
THEN
UTL_HTTP.end_response (http_resp);
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Error at reading raw: ' || SQLERRM);
END;
-- then do whatever you like with the blob
l_CLOB := encode_base64 (l_blob); --encode the binary dat ato BASE64Binary stream , please note that encode_base64 is a seperate function define din DB
final_blob := decode_base64 (l_CLOB); --Decode above base64binary, decode_base64 is a independent function
insert into XXATI_PO_CHEMSPEC_PDF VALUES (1, final_blob, 'test file' ); --want to store this BLOB in its original from i.e. PDF file , but this BLOB appears weired
--
UTL_HTTP.end_response (http_resp);
EXCEPTION
WHEN UTL_HTTP.end_of_body THEN
DBMS_OUTPUT.put_line('end of body');
UTL_HTTP.end_response(http_resp);
WHEN UTL_HTTP.TOO_MANY_REQUESTS THEN
UTL_HTTP.END_RESPONSE(http_resp);
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('others exception '||SQLERRM);
end;
----------------------------------------------------------
Here is sample response from webservice:
<S:Envelope xmlns:S="
http://www.w3.org/2003/05/soap-envelope" >
<S:Header>
<work:WorkContext xmlns:work="
http://oracle.com/weblogic/soap/workarea/" >rO0ABXdJABV3ZWJsb2dpYy5hcHAuQ0hFTVNQRUMAAADWAAAAI3dlYmxvZ2ljLndvcmthcmVhLlN0cmluZ1dvcmtDb250ZXh0AAMxLjYAAA==</work:WorkContext>
</S:Header>
<S:Body>
<ns2:getGradeInfoForRMResponse xmlns:ns2="
http://grades.web.chemspec.quality.allvac.com/" >
<return>
<avSpec>104P-AV</avSpec>
<rev>2</rev>
<pdfFile>
<xop:Include href="cid:f71f052c-09bc-4157-9655-e469cb759e75@example.jaxws.sun.com" xmlns:xop="
http://www.w3.org/2004/08/xop/include"/ >
</pdfFile>
<refDocs>
<docName>Test Doc 1</docName>
<docRev>1</docRev>
</refDocs>
<refDocs>
<docName>Test Doc 2</docName>
<docRev>1</docRev>
</refDocs>
<refDocs>
<docName>Test Doc 3</docName>
<docRev>2</docRev>
</refDocs>
<refDocs>
<docName>Test Doc 4</docName>
<docRev>3</docRev>
</refDocs>
</return>
</ns2:getGradeInfoForRMResponse>
</S:Body>
</S:Envelope>
---Then Binary data------
%PDF-1.4
%âãÏÓ
1 0 obj
<</BaseFont/Helvetica-Bold/Type/Font/Encoding/WinAnsiEncoding/Subtype/Type1>>
endobj
2 0 obj
<</Length 10/Filter/FlateDecode>>stream
±;û 8©^íJ
endstream
endobj
3 0 obj.........