Oracle8i Supplied Packages Reference Release 8.1.5 A68001-01 |
|
UTL_HTTP
makes hyper-text transfer protocol (HTTP) callouts from PL/SQL and SQL. You can use it to access data on the Internet or to call Oracle Web Server cartridges.
UTL_HTTP
contains two similar entrypoints: REQUEST
and REQUEST_PIECES
. They each take a string universal resource locator (URL), contact that site, and return the data (typically HTML -- hyper-text markup language) obtained from that site.
The above two exceptions, unless explicitly caught by an exception handler, are reported by this generic message: ORA-06510
: PL/SQL: unhandled user-defined exception. This reports them as "user-defined" exceptions, although they are defined in this system package.
If any other exception is raised during the processing of the HTTP request (for example, an out-of-memory error), then function REQUEST
or REQUEST_PIECES
reraises that exception.
When no response is received from a request to the given URL (for example, because no site corresponding to that URL is contacted), then a formatted HTML error message may be returned. For example:
<HTML> <HEAD> <TITLE>Error Message</TITLE> </HEAD> <BODY> <H1>Fatal Error 500</H1> Can't Access Document: http://home.nothing.comm. <P> <B>Reason:</B> Can't locate remote host: home.nothing.comm. <P> <P><HR> <ADDRESS><A HREF="http://www.w3.org"> CERN-HTTPD3.0A</A></ADDRESS> </BODY> </HTML>
You should not expect REQUEST
or REQUEST_PIECES
to succeed in contacting a URL unless you can contact that URL by using a browser on the same machine (and with the same privileges, environment variables, etc.)
If REQUEST
or REQUEST_PIECES
fails (for example, if it raises an exception, or if it returns an HTML-formatted error message, yet you believe that the URL argument is correct), then try contacting that same URL with a browser, to verify network availability from your machine. Remember that you may have a proxy server set in your browser that needs to be set with each REQUEST
or REQUEST_PIECES
call using the optional proxy
parameter.
This function returns up to the first 2000 bytes of the data retrieved from the given URL.
UTL_HTTP.REQUEST ( url IN VARCHAR2, proxy IN VARCHAR2 DEFAULT NULL) RETURN VARCHAR2;
pragma restrict_references (request, wnds, rnds, wnps, rnps);
Parameter | Description |
---|---|
url |
Universal resource locator. |
proxy |
(Optional) Specifies a proxy server to use when making the HTTP request. |
Its return-type is a string of length 2000 or less, which contains up to the first 2000 bytes of the HTML result returned from the HTTP request to the argument URL.
INIT_FAILED
REQUEST_FAILED
SVRMGR> SELECT utl_http.request('http://www.oracle.com/') FROM dual; UTL_HTTP.REQUEST('HTTP://WWW.ORACLE.COM/') <html> <head><title>Oracle Corporation Home Page</title> <!--changed Jan. 16, 19 1 row selected.
If you are behind a firewall, include the proxy
parameter. For example, from within the Oracle firewall, where there might be a proxy server named www-proxy
.us
.oracle
.com
:
SVRMGR> SELECT utl_http.request('http://www.oracle.com', 'www-proxy.us.oracle.com') FROM dual;
This function returns a PL/SQL table of 2000-byte pieces of the data retrieved from the given URL.
type html_pieces is table of varchar2(2000) index by binary_integer; UTL_HTTP.REQUEST_PIECES ( url IN VARCHAR2, max_pieces NATURAL DEFAULT 32767, proxy IN VARCHAR2 DEFAULT NULL) RETURN HTML_PIECES;
pragma restrict_references (request_pieces, wnds, rnds, wnps, rnps);
REQUEST_PIECES
returns a PL/SQL table of type UTL_HTTP
.HTML_PIECES
. Each element of that PL/SQL table is a string of length 2000. The final element may be shorter than 2000 characters.
The elements of the PL/SQL table returned by REQUEST_PIECES
are successive pieces of the data obtained from the HTTP request to that URL.
INIT_FAILED
REQUEST_FAILED
A call to REQUEST_PIECES
could look like the example below. Note the use of the PL/SQL table method COUNT
to discover the number of pieces returned, which may be zero or more:
DECLARE pieces utl_http.html_pieces; BEGIN pieces := utl_http.request_pieces('http://www.oracle.com/'); FOR i in 1 .. pieces.count loop .... -- process each piece END LOOP; END;
The following block retrieves up to 100 pieces of data (each 2000 bytes, except perhaps the last) from the URL. It prints the number of pieces retrieved and the total length, in bytes, of the data retrieved.
SET SERVEROUTPUT ON / DECLARE x utl_http.html_pieces; BEGIN x := utl_http.request_pieces('http://www.oracle.com/', 100); dbms_output.put_line(x.count || ' pieces were retrieved.'); dbms_output.put_line('with total length '); IF x.count < 1 THEN dbms_output.put_line('0'); ELSE dbms_output.put_line ((2000 * (x.count - 1)) + length(x(x.count))); END IF; END; / -- Output Statement processed. 4 pieces were retrieved. with total length 7687