Oracle9i Supplied PL/SQL Packages and Types Reference
Release 1 (9.0.1)

Part Number A89852-02
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback

Go to previous page Go to beginning of chapter Go to next page

UTL_HTTP , 2 of 59


UTL_HTTP Flow

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.

Figure 78-1 Flow of the Core UTL_HTTP Package


Text description of utl_htta.gif follows
Text description of the illustration utl_htta.gif

The following can be called at any time:

UTL_HTTP Exceptions

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).


Table 78-5 UTL_HTTP Exceptions
Exception  Error Code  Reason  Where Raised 

request_failed 

29273 

The request fails to executes 

Any HTTP request or response API when detailed_exception is disabled 

bad_argument 

29261 

The argument passed to the API is bad 

Any HTTP request or response API when detailed_exception is enabled 

bad_url 

29262 

The requested URL is badly formed 

begin_request, when detailed_exception is enabled 

protocol_error 

29263 

An HTTP protocol error occurs when communicating with the Web server 

set_header, get_response, read_raw, read_text, and read_line, when detailed_exception is enabled 

unknown_scheme 

29264 

The scheme of the requested URL is unknown 

begin_request and get_response, when detailed_exception is enabled 

header_not_found 

29265 

The header is not found 

get_header, get_header_by_name, when detailed_exception is enabled 

end_of_body 

29266 

The end of HTTP response body is reached 

read_raw, read_text, and read_line, when detailed_exception is enabled 

illegal_call 

29267 

The call to UTL_HTTP is illegal at the current state of the HTTP request 

set_header, set_authentication, and set_persistent_conn_support, when detailed_exception is enabled  

http_client_error 

29268 

The response status code indicates that a client error has occurred (status code in 4xx range) 

get_response, when detailed_exception is enabled  

http_server_error 

29269 

The response status code indicates that a client error has occurred (status code in 5xx range) 

get_response, when detailed_exception is enabled  

too_many_requests 

29270 

Too many requests or responses are open 

begin_request, when detailed_exception is enabled 

partial_multibyte_exception 

29275 

No complete character is read and a partial multi-byte character is found at the end of the response body  

read_text and read_line, when detailed_exception is enabled  

transfer_timeout 

29276 

No data is read and a read timeout occurred 

read_text and read_line, when detailed_exception is enabled  


NOTE:

The partial_multibyte_char and transfer_timeout exceptions are duplicates of the same exceptions defined in UTL_TCP. They are defined in this package so that the use of this package does not require the knowledge of the UTL_TCP. As those exceptions are duplicates, an exception handle that catches the partial_multibyte_char and transfer_timeout exceptions in this package also catch the exceptions in the UTL_TCP.  


For REQUEST and REQUEST_PIECES(), the request_failed exception is raised when any exception occurs and detailed_exception is disabled.

UTL_HTTP Examples

The following examples demonstrate how to use UTL_HTTP.

Example: Using 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;

Example: Retrieving HTTP Response Headers

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;

Example: Handling HTTP Authentication

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;

Example: Retrieving and Restoring Cookies

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;

Summary of UTL_HTTP Subprograms

Table 78-6 UTL_HTTP Subprograms  
Subprogram  Description 

Simple HTTP fetches in a single call 

"REQUEST Function" 

Returns up to the first 2000 bytes of the data retrieved from the given URL. This function can be used directly in SQL queries. 

"REQUEST_PIECES Function" 

Returns a PL/SQL table of 2000-byte pieces of the data retrieved from the given URL. 

Session Settings 

"SET_PROXY Procedure" 

Sets the proxy to be used for requests of HTTP or other protocols 

"GET_PROXY Procedure" 

Retrieves the current proxy settings 

"SET_COOKIE_SUPPORT Procedure" 

Sets whether or not future HTTP requests will support HTTP cookies; sets the maximum number of cookies maintained in the current database user session 

"GET_COOKIE_SUPPORT Procedure" 

Retrieves the current cookie support settings 

"SET_FOLLOW_REDIRECT Procedure" 

Sets the maximum number of times UTL_HTTP follows the HTTP redirect instruction in the HTTP responses to future requests in the get_response function 

"GET_FOLLOW_REDIRECT Procedure" 

Retrieves the follow-redirect setting in the current session 

"SET_BODY_CHARSET Procedure" 

Sets the default character set of the body of all future HTTP requests when the media type is text and the character set is not specified in the Content-Type header 

"GET_BODY_CHARSET Procedure" 

Retrieves the default character set of the body of all future HTTP requests 

"SET_PERSISTENT_CONN_SUPPORT Procedure" 

Sets whether or not future HTTP requests will support the HTTP 1.1 persistent connection; sets the maximum number of persistent connections maintained in the current database user session 

"GET_PERSISTENT_CONN_SUPPORT Procedure" 

Checks if the persistent connection support is enabled and gets the maximum number of persistent connections in the current session 

"SET_RESPONSE_ERROR_CHECK Procedure" 

Sets whether or not get_response raises an exception when the Web server returns a status code that indicates an error--a status code in the 4xx or 5xx ranges 

"GET_RESPONSE_ERROR_CHECK Procedure" 

Checks if the response error check is set or not 

"SET_DETAILED_EXCP_SUPPORT Procedure" 

Sets the UTL_HTTP package to raise a detailed exception 

"GET_DETAILED_EXCP_SUPPORT Procedure" 

Checks if the UTL_HTTP package will raise a detailed exception or not 

"SET_WALLET Procedure" 

Sets the Oracle Wallet used for all HTTP requests over Secured Socket Layer (SSL), that is, HTTPS 

"SET_TRANSFER_TIMEOUT Procedure" 

Sets the timeout value for UTL_HTTP to read the HTTP response from the Web server or proxy server 

"GET_TRANSFER_TIMEOUT Procedure" 

Retrieves the current network transfer timeout value 

HTTP Requests 

"BEGIN_REQUEST Function" 

Begins a new HTTP request. UTL_HTTP establishes the network connection to the target Web server or the proxy server and sends the HTTP request line.  

"SET_HEADER Procedure" 

Sets an HTTP request header. The request header is sent to the Web server as soon as it is set. 

"SET_AUTHENTICATION Procedure" 

Sets HTTP authentication information in the HTTP request header. The Web server needs this information to authorize the request. 

"SET_COOKIE_SUPPPORT Procedure" 

Enables or disables support for the HTTP cookies in the request.  

"SET_FOLLOW_REDIRECT Procedure" 

Sets the maximum number of times UTL_HTTP follows the HTTP redirect instruction in the HTTP response to this request in the GET_RESPONSE function. 

"SET_BODY_CHARSET Procedure" 

Sets the character set of the request body when the media type is text but the character set is not specified in the Content-Type header. 

"SET_PERSISTENT_CONN_SUPPORT Procedure" 

Enables or disables support for the HTTP 1.1 persistent-connection in the request. 

"WRITE_TEXT Procedure" 

Writes some text data in the HTTP request body.  

"WRITE_LINE Procedure" 

Writes a text line in the HTTP request body and ends the line with new-line characters (CRLF as defined in UTL_TCP). 

"WRITE_RAW Procedure" 

Writes some binary data in the HTTP request body. 

"END_REQUEST Procedure" 

Ends the HTTP request. 

HTTP Responses 

 

"GET_RESPONSE Function" 

Reads the HTTP response. When the function returns, the status line and the HTTP response headers have been read and processed. 

"GET_HEADER_COUNT Function" 

Returns the number of HTTP response headers returned in the response. 

"GET_HEADER Procedure" 

Returns the nth HTTP response header name and value returned in the response. 

"GET_HEADER_BY_NAME Procedure" 

Returns the HTTP response header value returned in the response given the name of the header. 

"GET_AUTHENTICATION Procedure" 

Retrieves the HTTP authentication information needed for the request to be accepted by the Web server as indicated in the HTTP response header. 

"SET_BODY_CHARSET Procedure" 

Sets the character set of the response body when the media type is "text" but the character set is not specified in the "Content-Type" header. 

"READ_TEXT Procedure" 

Reads the HTTP response body in text form and returns the output in the caller-supplied buffer.  

"READ_LINE Procedure" 

Reads the HTTP response body in text form until the end of line is reached and returns the output in the caller-supplied buffer.  

"READ_RAW Procedure" 

Reads the HTTP response body in binary form and returns the output in the caller-supplied buffer.  

"END_RESPONSE Procedure" 

Ends the HTTP response. It completes the HTTP request and response.  

HTTP Cookies 

 

"GET_COOKIE_COUNT Function" 

Returns the number of cookies currently maintained by the UTL_HTTP package set by all Web servers. 

"GET_COOKIES Function" 

Returns all the cookies currently maintained by the UTL_HTTP package set by all Web servers. 

"ADD_COOKIES Procedure" 

Adds the cookies maintained by UTL_HTTP

"CLEAR_COOKIES Procedure" 

Clears all cookies maintained by the UTL_HTTP package. 

HTTP Persistent Connections 

 

"GET_PERSISTENT_CONN_COUNT Function" 

Returns the number of network connections currently kept persistent by the UTL_HTTP package to the Web servers. 

"GET_PERSISTENT_CONNS Procedure" 

Returns all the network connections currently kept persistent by the UTL_HTTP package to the Web servers. 

"CLOSE_PERSISTENT_CONN Procedure" 

Closes an HTTP persistent connection maintained by the UTL_HTTP package in the current database session. 

"CLOSE_PERSISTENT_CONNS Procedure" 

Closes a group of HTTP persistent connections maintained by the UTL_HTTP package in the current database session. 

Error Conditions 

 

"GET_DETAILED_SQLCODE Function" 

Retrieves the detailed SQLCODE of the last exception raised. 

"GET_DETAILED_SQLERRM Function" 

Retrieves the detailed SQLERRM of the last exception raised. 


Go to previous page Go to beginning of chapter Go to next page
Oracle
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback