Oracle 使用UTL_HTTP发送http请求--转载
参考:https://blog.csdn.net/tmaczt/article/details/82665885
GET方式
CREATE OR REPLACE FUNCTION FN_HTTP_GET (v_url VARCHAR2)
RETURN VARCHAR2
AS
BEGIN
DECLARE
req UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
v_line VARCHAR2 ( 4000 );
v_text VARCHAR2 ( 4000 );
BEGIN
v_text := '';
BEGIN
req := UTL_HTTP.BEGIN_REQUEST ( url => v_url, method => 'GET' );
UTL_HTTP.SET_BODY_CHARSET('UTF-8');
UTL_HTTP.SET_HEADER(req, 'Content-Type', 'application/x-www-form-urlencoded');
resp := UTL_HTTP.GET_RESPONSE ( req );
LOOP
UTL_HTTP.READ_LINE ( resp, v_line, TRUE );
v_text := v_text || v_line;
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 );
WHEN OTHERS THEN
UTL_HTTP.END_RESPONSE(resp);
UTL_HTTP.END_REQUEST(req);
END;
return v_text;
END;
END;
测试
--测试http get请求
select fn_http_get('http://192.168.43.190:8080/user/hello') from dual;
select fn_http_get('http://192.168.43.190:8080/param/testParam?username=%E6%9C%B1%E4%BF%8A%E4%BC%9F'||'&'||'password=123') from dual;
结果
POST请求
CREATE OR REPLACE FUNCTION FN_HTTP_POST (v_url VARCHAR2, v_body VARCHAR2, v_body_type VARCHAR2)
RETURN VARCHAR2
AS
BEGIN
DECLARE
req UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
v_line VARCHAR2 ( 4000 );
v_text VARCHAR2 ( 4000 );
BEGIN
v_text := '';
BEGIN
req := UTL_HTTP.BEGIN_REQUEST ( url => v_url, method => 'POST' );
UTL_HTTP.SET_BODY_CHARSET('UTF-8');
UTL_HTTP.SET_HEADER(req, 'Content-Type', v_body_type);
utl_http.set_header(req, 'Content-Length',lengthb(v_body));
utl_http.write_text(req, v_body);
resp := UTL_HTTP.GET_RESPONSE ( req );
LOOP
UTL_HTTP.READ_LINE ( resp, v_line, TRUE );
v_text := v_text || v_line;
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 );
WHEN OTHERS THEN
UTL_HTTP.END_RESPONSE(resp);
UTL_HTTP.END_REQUEST(req);
END;
return v_text;
END;
END;
测试
--测试http post请求
select fn_http_post('http://192.168.43.190:8080/param/saveAccount','username=李四'||'&'||'password=123456'||'&'||'money=100','application/x-www-form-urlencoded') from dual;
结果
---------------
我每一次回头,都感觉自己不够努力,所以我不再回头。
---------------