标签:
在我们开发plsql程序的过程中,有时候难免要访问一些外部网站的数据。这个时候我们就要用到utl_http包。
使用utl_http包前需要注意的是,当前的用户下是否有访问外部网络的权限。
如下是自己总结的函数,欢迎大家交流学习。
get方式:
1 function http_get(p_url in varchar2) return clob 2 is 3 http_req utl_http.req; 4 http_resp utl_http.resp; 5 l_raw raw(1024); 6 l_r clob; 7 begin 8 begin 9 http_req:=utl_http.begin_request(p_url,‘GET‘); 10 http_resp := utl_http.get_response(http_req, TRUE); 11 loop 12 utl_http.read_raw(http_resp, l_raw,1024); 13 l_r:=l_r||utl_raw.cast_to_varchar2(l_raw); 14 end loop; 15 utl_http.end_response(http_resp); 16 exception 17 when utl_http.end_of_body then 18 utl_http.end_response(http_resp); 19 end; 20 return l_r; 21 end;
post方式:
1 function http_post( 2 p_url in varchar2, 3 p_data in varchar2 --a=1&b=2... 4 ) return clob 5 is 6 http_req utl_http.req; 7 http_resp utl_http.resp; 8 l_raw raw(1024); 9 l_r clob; 10 begin 11 begin 12 http_req:=utl_http.begin_request(p_url,‘POST‘); 13 utl_http.set_header(http_req,‘Content-Type‘,‘application/x-www-form-urlencoded;charset=utf-8‘); 14 utl_http.set_header(http_req,‘Content-Length‘,length(p_data)); 15 utl_http.write_text(http_req,p_data); 16 http_resp := utl_http.get_response(http_req, TRUE); 17 loop 18 utl_http.read_raw(http_resp, l_raw,1024); 19 l_r:=l_r||utl_raw.cast_to_varchar2(l_raw); 20 end loop; 21 utl_http.end_response(http_resp); 22 exception 23 when utl_http.end_of_body then 24 utl_http.end_response(http_resp); 25 end; 26 return l_r; 27 end;
The END.
标签:
原文地址:http://www.cnblogs.com/shepherd09/p/4429019.html