PL/SQL webpage access (get or post), plsql
When we develop a plsql program, it is inevitable that we sometimes access data from some external websites. At this time, we need to use the utl_http package.
Before using the utl_http package, check whether the current user has the permission to access the external network.
The following is a summary of the functions. You are welcome to exchange and learn.
Get method:
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 loop12 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 exception17 when utl_http.end_of_body then18 utl_http.end_response(http_resp);19 end;20 return l_r; 21 end;
Post method:
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 begin11 begin12 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 loop18 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 exception23 when utl_http.end_of_body then24 utl_http.end_response(http_resp);25 end;26 return l_r; 27 end;
The END.