How to Read Web Pages Using utl_http.request_pieces
UTL_HTTP package provides easy to use methods to fetch data from web using http protocol. Today, one of my co-workers complained that “utl_http.request” method returns only a small part of the web page instead of full html. She says she already uses it to fetch from other web pages without any problem. The utl_http.request method returns the requested page as a single string. When I check the documents, I see that it intentionally returns only first 2000 characters, and we should use “utl_http.request_pieces” method if we need to fetch more than 2000 characters. The UTL_HTTP.REQUEST_PIECES returns “utl_http.html_pieces” which is defined as a PLSQL table of 2000 character strings:
1 |
TYPE html_pieces IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER; |
So I created a small function to demonstrate how we can fetch a web page using utl_http.request_pieces:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
CREATE OR REPLACE FUNCTION readfromweb (url VARCHAR2) RETURN CLOB IS pcs UTL_HTTP.html_pieces; retv CLOB; BEGIN pcs := UTL_HTTP.request_pieces (url, 50); FOR i IN 1 .. pcs.COUNT LOOP retv := retv || pcs (i); END LOOP; RETURN retv; END; |
And tested it:
1 |
SELECT readfromweb ('http://..../awebpage.aspx') FROM DUAL; |