Oracle9i Supplied PL/SQL Packages and Types Reference Release 1 (9.0.1) Part Number A89852-02 |
|
UTL_HTTP , 2 of 59
The UTL_HTTP
package provides access to the HTTP protocol. The API must be called in the order shown, or an exception will be raised.
The following can be called at any time:
UTL_HTTP
package in the current session
UTL_HTTP
package in the current session
Table 78-5 lists the exceptions that the UTL_HTTP
package API can raise. By default, UTL_HTTP
raises the exception request_failed
when a request fails to execute. If the package is set to raise a detailed exception by set_detailed_excp_support
, the rest of the exceptions will be raised directly (except for the exception end_of_body
, which will be raised by read_text
, read_line
, and read_raw
regardless of the setting).
For REQUEST and REQUEST_PIECES(), the request_failed
exception is raised when any exception occurs and detailed_exception
is disabled.
The following examples demonstrate how to use UTL_HTTP.
SET serveroutput ON SIZE 40000 DECLARE req utl_http.req; resp utl_http.resp; value VARCHAR2(1024); BEGIN utl_http.set_proxy('proxy.my-company.com', 'corp.my-company.com'); req := utl_http.begin_request('http://www-hr.corp.my-company.com'); utl_http.set_header(req, 'User-Agent', 'Mozilla/4.0'); resp := utl_http.get_response(req); LOOP utl_http.read_line(resp, value, TRUE); dbms_output.put_line(value); END LOOP; utl_http.end_response(resp); EXCEPTION WHEN utl_http.end_of_body THEN utl_http.end_response(resp); END;
SET serveroutput ON SIZE 40000 DECLARE req utl_http.req; resp utl_http.resp; name VARCHAR2(256); value VARCHAR2(1024); BEGIN utl_http.set_proxy('proxy.my-company.com', 'corp.my-company.com'); req := utl_http.begin_request('http://www-hr.corp.my-company.com'); utl_http.set_header(req, 'User-Agent', 'Mozilla/4.0'); resp := utl_http.get_response(req); dbms_output.put_line('HTTP response status code: ' || resp.status_code); dbms_output.put_line('HTTP response reason phrase: ' || resp.reason_phrase); FOR i IN 1..utl_http.get_header_count(resp) LOOP utl_http.get_header(resp, i, name, value); dbms_output.put_line(name || ': ' || value); END LOOP; utl_http.end_response(resp); END;
SET serveroutput ON SIZE 40000 CREATE OR REPLACE PROCEDURE get_page (url IN VARCHAR2, username IN VARCHAR2 DEFAULT NULL, password IN VARCHAR2 DEFAULT NULL, realm IN VARCHAR2 DEFAULT NULL) AS req utl_http.req; resp utl_http.resp; my_scheme VARCHAR2(256); my_realm VARCHAR2(256); my_proxy BOOLEAN; BEGIN -- Turn off checking of status code. We will check it by ourselves. utl_http.http_response_error_check(FALSE); req := utl_http.begin_request(url); IF (username IS NOT NULL) THEN utl_http.set_authentication(req, username, password); -- Use HTTP Basic Authen. Scheme END IF; resp := utl_http.get_response(req); IF (resp.status_code = utl_http.HTTP_UNAUTHORIZED) THEN utl_http.get_authentication(resp, my_scheme, my_realm, my_proxy); IF (my_proxy) THEN dbms_output.put_line('Web proxy server is protected.'); dbms_output.put('Please supplied the required ' || my_scheme || ' authentication username/password for realm ' || my_realm || ' for the proxy server.'); ELSE dbms_output.put_line('Web page ' || url || ' is protected.'); dbms_output.put('Please supplied the required ' || my_scheme || ' authentication username/password for realm ' || my_realm || ' for the Web page.'); END IF; utl_http.end_response(resp); RETURN; END IF; FOR i IN 1..utl_http.get_header_count(resp) LOOP utl_http.get_header(resp, i, name, value); dbms_output.put_line(name || ': ' || value); END LOOP; utl_http.end_response(resp); END;
CREATE TABLE my_cookies ( session_id BINARY_INTEGER, name VARCHAR2(256), value VARCHAR2(1024), domain VARCHAR2(256), expire DATE, path VARCHAR2(1024), secure VARCHAR2(1), version BINARY_INTEGER ); CREATE SEQUENCE session_id; SET serveroutput ON SIZE 40000 REM Retrieve cookies from UTL_HTTP CREATE OR REPLACE FUNCTION save_cookies RETURN BINARY_INTEGER AS cookies utl_http.cookie_table; my_session_id BINARY_INTEGER; secure VARCHAR2(1); BEGIN /* assume that some cookies have been set in previous HTTP requests. */ utl_http.get_cookies(cookies); select session_id.nextval into my_session_id from dual; FOR i in 1..cookies.count LOOP IF (cookies(i).secure) THEN secure := 'Y'; ELSE secure := 'N'; END IF; insert into my_cookies value (my_session_id, cookies(i).name, cookies(i).value, cookies(i).domain, cookies(i).expire, cookies(i).path, secure, cookies(i).version); END LOOP; RETURN my_session_id; END; REM Retrieve cookies from UTL_HTTP CREATE OR REPLACE PROCEDURE restore_cookies (this_session_id IN BINARY_INTEGER) AS cookies utl_http.cookie_table; cookie utl_http.cookie; i PLS_INTEGER := 0; CORSOR c (c_session_id BINARY_INTEGER) IS SELECT * FROM my_cookies WHERE session_id = c_session_id; BEGIN FOR r IN c(this_session_id) LOOP i := i + 1; cookie.name := r.name; cookie.value := r.value; cookie.domain := r.domain; cookie.expire := r.expire; cookie.path := r.path; IF (r.secure = 'Y') THEN cookie.secure := TRUE; ELSE cookie.secure := FALSE; END IF; cookie.version := r.version; cookies(i) := cookie; END LOOP; utl_http.clear_cookies; utl_http.add_cookies(cookies); END;
|
Copyright © 1996-2001, Oracle Corporation. All Rights Reserved. |
|