使用UTL_HTTP包获取网页内容

UTL_HTTP 包提供了容易的方式通过HTTP协议获取网页内容,下面结合几个例子介绍一下:

----------------------------------------------------------------------------------------

1、小网页内容获取(<2000 bytes):

  1.1 创建函数p,供输出获取到的网页数据使用:

create or replace procedure p(p_string in varchar2) is
    l_string long default p_string;
begin
    loop
        exit when l_string is null;
        dbms_output.put_line(substr(l_string, 1, 250));
        l_string := substr(l_string, 251);
    end loop;
end;
  1.2 获取网页内容函数:

declare
      l_page    long;
      l_url    varchar2(35) default   'http://www.baidu.com/';
  begin
      l_page := utl_http.request( l_url );

      p( l_page );
  end;
  /

2、超过2000 bytes内容获取:(使用request_pieces)

declare
     l_page    utl_http.html_pieces;
     l_url     varchar2(25) default 'www.baidu.com';
 begin
     l_page := utl_http.request_pieces( l_url,
                                        50 );
 
     for i in 1 .. l_page.count
     loop
         p( l_page(i) );
     end loop;
 end;

CREATE OR REPLACE FUNCTION readfromweb(url VARCHAR2) RETURN CLOB IS
    --TYPE html_pieces IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER;
    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;

--官方例子:

declare
    x   utl_http.html_pieces;
    len pls_integer;
  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 ');
    len := 0;
    for i in 1..x.count loop
      len := len + length(x(i));
    end loop;
    dbms_output.put_line(len);
  end;
Here is the output:
  Statement processed.
  4 pieces were retrieved.
  with total length
  7687

3、ORA-24247问题解决,参考:

点击打开链接 http://blog.csdn.net/indexman/article/details/17048677

















--------------------------------

Dylan presents.

posted @ 2013-11-30 22:57  一锤子技术员  阅读(17)  评论(0编辑  收藏  举报  来源