Oracle通过http协议发送post请求


定义ACL,若没有ACL,则无法访问网络。

--定义ACL 取名:httprequestpermission.xml

BEGIN
dbms_network_acl_admin.create_acl(acl => 'httprequestpermission.xml',
DESCRIPTION => 'Normal Access',
principal => 'CONNECT',
is_grant => TRUE,
PRIVILEGE => 'connect',
start_date => NULL,
end_date => NULL);
END;

--查看ACL是否增加成功
SELECT *
FROM resource_view
where any_path like '/sys/acls/%.xml' ;

--给用户增加acl权限,这里是 YLXWJK 注意是大写,小写不识别
begin dbms_network_acl_admin.add_privilege(acl => 'httprequestpermission.xml',
principal => 'YLXWJK',
is_grant => TRUE,
privilege => 'connect',
start_date => null,
end_date => null);
end;

--添加对应主机 ,将对应主机和端口添加到ACL。这里是 192.168.0.156 和 8080 ,这个ip和端口要和上面存储过程中定义的地址一致
begin
dbms_network_acl_admin.assign_acl(acl => 'httprequestpermission.xml',
host => '172.17.5.153',
lower_port => 8080,
upper_port => NULL);
end;

请求范例1:v_content 为json格式字符串,requrl为请求地址

create or replace procedure sendhttp(requrl in varchar2,
v_content in varchar2) is
req utl_http.req;
resp utl_http.resp;
v_return varchar2(8000); -- 存放返回值
v_pk varchar2(32); -- 存放主键列
v_tradeno varchar2(4); -- 存放交易号
v_idno varchar2(18); -- 存放身份证号码
v_err_msg varchar2(2048); -- 存放异常信息

begin
begin
req := utl_http.begin_request(url => requrl,
method => 'POST');
utl_http.set_body_charset('utf-8');--utf-8 返回中文会乱码
utl_http.set_header(req, 'Content-Type', 'application/json');
utl_http.set_header(req,
'Content-Length',
lengthb(v_content));
-- utl_http.write_raw(req,UTL_RAW.cast_to_raw(v_content));
utl_http.write_text(req, v_content);
resp := utl_http.get_response(req);
loop
utl_http.read_text(resp, v_return);
---utl_http.read_line(resp, v_return, true);
v_return :=substr(v_return,1,800);
dbms_output.put_line(v_return);
dbms_output.put_line(parsejson(v_return,'data'));
dbms_output.put_line(parsejson(v_return,'akc225'));
dbms_output.put_line(parsejson(v_return,'ape139'));
if v_return is not null then
exit;
end if;
end loop;
utl_http.end_response(resp);

exception
when others then
--- v_err_msg :=SUBSTR(SQLERRM, 1, 512);
utl_http.end_response(resp);
v_err_msg := sqlerrm;

end;
end sendhttp;

请求范例2:v_content 为json格式字符串,parsejson(v_return,'akc225') 为取json字符串的某个值方法

create or replace procedure p_yd_tbkkssxx(requrl in varchar2) is
req utl_http.req;
resp utl_http.resp;
v_return varchar2(8000); -- 存放返回值
v_err_msg varchar2(2048); -- 存放异常信息
v_content varchar2(2048);
aae030 varchar2(7);
aae031 varchar2(7);
bke045 varchar2(20);
ake007 varchar2(8);
begin
begin
for curl_1 in(select f.aaa999,f.aae072,f.pid from fx_ydxxb0 f
where f.clzt in('4','5') )loop
select y.bke045 into bke045 from ybdy.kc22_mz@fzyb y where y.aaz213=curl_1.aaa999;
select y.ake007 into ake007 from ybdy.kc22_mz@fzyb y where y.aaz213=curl_1.aaa999;
aae030 := to_char(add_months(to_date(substr(ake007,'1','6'),'yyyyMM'),-1),'yyyy-MM');
aae031 := to_char(add_months(to_date(substr(ake007,'1','6'),'yyyyMM'),-1),'yyyy-MM');
v_content :='{
"aae030": "'||aae030||'",
"aae031": "'||aae031||'",
"page": 1,
"pageSiz": 10,
"pwd": "123",
"uname": "123",
"ake001":"'||bke045||'",
"djlsh0":"'||curl_1.aae072||'"

}';
req := utl_http.begin_request(url => requrl,
method => 'POST');
utl_http.set_body_charset('utf-8');--utf-8 返回中文会乱码
utl_http.set_header(req, 'Content-Type', 'application/json');
utl_http.set_header(req,
'Content-Length',
lengthb(v_content));
-- utl_http.write_raw(req,UTL_RAW.cast_to_raw(v_content));
utl_http.write_text(req, v_content);
resp := utl_http.get_response(req);
loop
utl_http.read_text(resp, v_return);
v_return :=substr(v_return,1,800);
if parsejson(v_return,'akc225') is not null and parsejson(v_return,'ape139') is null then
update fx_ydxxb0 set clzt='5' where pid=curl_1.pid;--已扣款
commit;
elsif parsejson(v_return,'akc225') is not null and parsejson(v_return,'ape139') is not null then
update fx_ydxxb0 set clzt='6' where pid=curl_1.pid;--已申诉
commit;
end if;
if v_return is not null then
exit;
end if;
end loop;
utl_http.end_response(resp);
end loop;
exception
when others then
--- v_err_msg :=SUBSTR(SQLERRM, 1, 512);
utl_http.end_response(resp);
v_err_msg := sqlerrm;
end;
end p_yd_tbkkssxx;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM