oracle存储过程调用http接口

创建ACL


-- 添加acl和权限控制(sql语句执行的方式来执行)
	begin
	  dbms_network_acl_admin.create_acl (       -- 创建访问控制文件(ACL)
		acl         => 'utl_http.xml',          -- 文件名称
		description => 'HTTP Access',           -- 描述
		principal   => 'N2ADMIN',             -- 授权或者取消授权账号,大小写敏感
		is_grant    => TRUE,                    -- 授权还是取消授权
		privilege   => 'connect',               -- 授权或者取消授权的权限列表
		start_date  => null,                    -- 起始日期
		end_date    => null                     -- 结束日期
	  );
	 
	  dbms_network_acl_admin.add_privilege (    -- 添加访问权限列表项
		acl        => 'utl_http.xml',           -- 刚才创建的acl名称 
		principal  => 'N2ADMIN',                    -- 授权或取消授权用户
		is_grant   => TRUE,                     -- 与上同 
		privilege  => 'resolve',                -- 权限列表
		start_date => null,                     
		end_date   => null
	  );
	 
	  dbms_network_acl_admin.assign_acl (       -- 该段命令意思是允许访问acl名为utl_http.xml下授权的用户,使用oracle网络访问包,所允许访问的目的主机,及其端口范围。
		acl        => 'utl_http.xml',
		host       => '10.1.131.71',           -- ip地址或者域名,填写https://localhost:9000/hello与https://localhost:9000/是会报host无效的
												-- 且建议使用ip地址或者使用域名,若用localhost,当oracle不是安装在本机上的情况下,会出现问题
		lower_port => 8080,                     -- 允许访问的起始端口号
		upper_port => Null                      -- 允许访问的截止端口号
	  );
	  commit;
	end;

存储过程调用http接口

CREATE OR REPLACE
procedure p_c_web(
										
                    canshu       in  VARCHAR2,
                    res          out  VARCHAR2
                 )
as
  L_RES varchar2(100);
  L_VENDOR_ID varchar2(100);
  L_INVENTORY_ITEM_ID  varchar2(100);
begin
    declare

    req    utl_http.req;
    resp   utl_http.resp;
    v_line varchar2(4000);
    v_text varchar2(4000);
    l_body varchar2(4000);
    l_res  varchar2(200);
    resvalue varchar2(200);
    L_ADD  varchar2(400);
  begin

    begin
    /**
		  调用前创建acl
      BEGIN
        DBMS_NETWORK_ACL_ADMIN.create_acl (
          acl          => 'open_acl_file.xml', 
          description  => 'Open ACL File',
          principal    => 'N2ADMIN',
          is_grant     => TRUE, 
          privilege    => 'connect',
          start_date   => SYSTIMESTAMP,
          end_date     => NULL);

        DBMS_NETWORK_ACL_ADMIN.assign_acl (
          acl         => 'open_acl_file.xml',
          host        => '*', 
          lower_port  => 1,
          upper_port  => 9999); 

        COMMIT;
      END;
      */
      l_body := '{"test":"test"}';
        
        -- SELECT HTTP_ADDRESS into L_ADD FROM T_HTTP_ADDRESS WHERE HTTP_CODE='9';
        
        
      L_ADD:='http://10.3.131.71:8080/N2/http/interface.ms?model=TclTests&method=TclTests';
        
      req := utl_http.begin_request(L_ADD,'POST');
      utl_http.set_body_charset('UTF-8');
      utl_http.set_header(req, 'Content-Type', 'application/json');
      utl_http.set_header(req, 'Content-Length', lengthb(l_body));
      utl_http.write_text(req, l_body);
      resp := utl_http.get_response(req);
      -- dbms_output.put_line(resp);
      loop
        utl_http.read_line(resp, v_line);
        v_text := v_text || v_line;
        
        dbms_output.put_line(l_body);     
        dbms_output.put_line(v_text); 
          
        
       -- select json_value(v_line,'$.body.DetailofWarehouseOUTResponse') into l_res from dual;

      end loop;
			
			
      utl_http.end_response(resp);
      utl_http.end_request(req);
		
    exception
      when utl_http.end_of_body then
        utl_http.end_response(resp);
				L_RES:='OK';
					res:=L_RES;
      when others then
				L_RES:='NG';
				res:=L_RES;
        dbms_output.put_line(sqlerrm);
        utl_http.end_response(resp);
        utl_http.end_request(req);
    end;


  end;
end;

你可能感兴趣的:(oracle)