Oracle調用HTTP接口

 1 CREATE OR REPLACE FUNCTION HTTP_REQUEST(
 2 v_url  VARCHAR2,--請求地址
 3 v_param  VARCHAR2,--POST請求參數-->>'muser=32323&&passwd=232323';GET拼接URL
 4 v_type  varchar2--類型
 5 )return varchar2 is
 6 --GET--
 7  req UTL_HTTP.REQ;
 8  resp UTL_HTTP.RESP;
 9  v_line VARCHAR2 ( 4000 );
10  v_text VARCHAR2 ( 4000 );
11  v_param_length NUMBER ;
12 --post--
13 begin
14     IF V_TYPE='GET' THEN  
15           v_text := '';
16           req := UTL_HTTP.BEGIN_REQUEST ( url => v_url, method => 'GET' );
17           UTL_HTTP.SET_BODY_CHARSET('UTF-8');
18           UTL_HTTP.SET_HEADER(req, 'Content-Type', 'application/x-www-form-urlencoded');
19           resp := UTL_HTTP.GET_RESPONSE ( req );
20           UTL_HTTP.READ_LINE ( resp, v_line, TRUE );
21           v_text := v_text || v_line;
22           UTL_HTTP.END_RESPONSE( resp );  
23         
24     ELSE
25           v_param_length  := LENGTHB(v_param);
26           req := UTL_HTTP.BEGIN_REQUEST (url=> v_url, method => 'POST');
27 
28           UTL_HTTP.SET_BODY_CHARSET('UTF-8');
29           UTL_HTTP.SET_HEADER (r      =>  req,
30                                name   =>  'Content-Type',
31                                VALUE  =>  'application/x-www-form-urlencoded');
32           UTL_HTTP.SET_HEADER (r      =>   req,
33                                name   =>   'Content-Length',
34                                VALUE  =>   v_param_length);
35           UTL_HTTP.WRITE_RAW (r    => req,
36                               data => UTL_RAW.CAST_TO_RAW(v_param)); 
37                               resp := UTL_HTTP.GET_RESPONSE(req);
38           UTL_HTTP.READ_LINE(resp, v_text, TRUE);
39           UTL_HTTP.END_RESPONSE(resp);
40     END IF;
41     return v_text;
42 EXCEPTION
43             WHEN UTL_HTTP.END_OF_BODY THEN
44             UTL_HTTP.END_RESPONSE ( resp );
45             WHEN OTHERS THEN
46             UTL_HTTP.END_RESPONSE(resp);
47             UTL_HTTP.END_REQUEST(req);
48 end;

猜你喜欢

转载自www.cnblogs.com/gany-4956/p/Oracle_htttprequest.html
今日推荐