使用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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值