第一步:
创建访问控制文件(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;