oracle通过存储调用http服务——POST方式

第一步:

创建访问控制文件(ACL):

--sysdba用户执行如下命令
--创建访问控制文件(ACL)
begin
  dbms_network_acl_admin.create_acl (
  acl => 'UTL_HTTP.xml',
  description => 'utl_http',
  principal => 'SCOTT',        -- 用户名 
  is_grant => TRUE,
  privilege => 'resolve'
  );
  commit;
 end;
/

--添加访问权限列表项
begin
  dbms_network_acl_admin.add_privilege (
  acl => 'UTL_HTTP.xml',
  principal => 'SCOTT',
  is_grant => TRUE,
  privilege => 'connect'
  );
  commit;
end;
/

--配置对应ip地址
begin
  dbms_network_acl_admin.assign_acl(
  acl => 'UTL_HTTP.xml',
  host => '192.168.123.xxx'         -- ip地址或者域名,建议使用ip地址或者使用域名,若用localhost,当oracle不是安装在本机上的情况下,会出现问题
  );
  commit;
end;
/

第二步:

写具体的实现存储过程:

create or replace procedure sp_http_post(av_url      in varchar2,
                                         av_request  in varchar2,
                                         av_response out varchar2) is
  l_http_request  utl_http.req;
  l_http_response utl_http.resp;
  l_clob         varchar2(32767);
  l_text          varchar2(32767);
begin


  -- 发送http请求UTL_HTTP.http_version_1_1
  l_http_request := utl_http.begin_request('http://218.26.228.117:8061/essc-service/api/essc/querySignNo', 'POST');
  utl_http.set_body_charset(l_http_request, 'UTF-8');
  --utl_http.set_header(l_http_request, 'Content-Type','application/x-www-form-urlencoded'); 
  utl_http.set_header(l_http_request, 'Content-Type', 'application/json');
  utl_http.set_header(l_http_request, 'Content-Length', LENGTH(av_request));
  --utl_http.write_text(l_http_request, av_request);
  utl_http.WRITE_RAW (l_http_request,UTL_RAW.CAST_TO_RAW(av_request));
  l_http_response := utl_http.get_response(l_http_request);

  if l_http_response.status_code = 200 then

     utl_http.read_text(l_http_response, av_response, length(av_response));

     utl_http.end_response(l_http_response);

   else--错误处理

     av_response := '网络访问错误!';

   utl_http.end_response(l_http_response);

   end if;

    exception--异常处理

     when others then

      dbms_output.put_line(sqlerrm);

      dbms_output.put_line(dbms_utility.format_error_backtrace);

      av_response := sqlerrm;

      if l_http_response.status_code is not null then

      utl_http.end_response(l_http_response);

      end if;
end;
--end;

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值