-- The following block shows: how to send an HTTP request, setting the proxy
information,
-- setting the method to "GET", providing username/password authentication
information,
-- and setting the request header; and how to get the response, retrieving
the status code,
-- the header information, and the response body.
-- The "GET" method is suitable for non-parameterized URLs or for
URLs with a manageable
--volume of parameter name-value pairs. The maximum length of the URL string
is
-- limited by the capacity of the PL/SQL VARCHAR2 variable used to pass it.
-- The "POST" method is suitable for parameterizing the request
with an arbitrarily
-- large volume of data, especially for example as might be the case when
the request
-- is expressed as an XML document.
-- Connect programmer/p@9i SET SERVEROUTPUT ON SIZE 1000000
declare
req Utl_Http.Req;
resp Utl_Http.Resp;
name varchar2(255);
value varchar2(1023);
v_msg varchar2(80);
v_url varchar2(32767) := '/';
begin
/* request that exceptions are raised for error Status Codes */
Utl_Http.Set_Response_Error_Check ( enable => true );
/* allow testing for exceptions like Utl_Http.Http_Server_Error */
Utl_Http.Set_Detailed_Excp_Support ( enable => true );
Utl_Http.Set_Proxy (
proxy => 'www-proxy.us.oracle.com',
no_proxy_domains => 'us.oracle.com' );
req := Utl_Http.Begin_Request (
url => v_url,
method => 'GET' );
/*
Alternatively use method => 'POST' and Utl_Http.Write_Text to
build an arbitrarily long message
*/
Utl_Http.Set_Authentication (
r => req,
username => 'SomeUser',
password => 'SomePassword',
scheme => 'Basic',
for_proxy => false /* this info is for the target web server */ );
Utl_Http.Set_Header (
r => req,
name => 'User-Agent',
value => 'Mozilla/4.0' );
resp := Utl_Http.Get_Response ( r => req );
Dbms_Output.Put_Line ( 'Status code: ' || resp.status_code ); Dbms_Output.Put_Line ( 'Reason phrase: ' || resp.reason_phrase );
for i in 1..Utl_Http.Get_Header_Count ( r => resp )
loop
Utl_Http.Get_Header (
r => resp,
n => i,
name => name,
value => value );
Dbms_Output.Put_Line ( name || ': ' || value);
end loop;
begin
loop
Utl_Http.Read_Text (
r => resp,
data => v_msg );
Dbms_Output.Put_Line ( v_msg );
end loop;
exception when Utl_Http.End_Of_Body then null;
end;
Utl_Http.End_Response ( r => resp );
exception
/*
The exception handling illustrates the use of "pragma-ed" exceptions
like Utl_Http.Http_Client_Error. In a realistic example, the program
would use these when it coded explicit recovery actions.
Request_Failed is raised for all exceptions after calling
Utl_Http.Set_Detailed_Excp_Support ( enable=>false )
And it is NEVER raised after calling with enable=>true
*/
when Utl_Http.Request_Failed then
Dbms_Output.Put_Line ( 'Request_Failed: ' || Utl_Http.Get_Detailed_Sqlerrm );
/* raised by URL http://xxx.oracle.com/ */
when Utl_Http.Http_Server_Error then
Dbms_Output.Put_Line ( 'Http_Server_Error: ' || Utl_Http.Get_Detailed_Sqlerrm );
/* raised by URL /xxx */
when Utl_Http.Http_Client_Error then
Dbms_Output.Put_Line ( 'Http_Client_Error: ' || Utl_Http.Get_Detailed_Sqlerrm );
/* code for all the other defined exceptions you can recover from */
when others then
Dbms_Output.Put_Line (SQLERRM);
end;
/
Show Errors