PLSQL中的使用utl_http来调用webservice

文章概要:

KingbaseES提供的utl_http能够支持对webservice的调用,这也是该接口的最重要的应用方式之一。本文基于POST协议的方式展示了webservice接口的简单调用例子。

一,搭建环境

本文需要基于一个写好的webservice发布包,在windows的IIS管理器上部署。

首先需要下载一个webservice服务端的发布包,下载链接如下,这个包是使用visual studio 2019创建的ASP.NET Web应用程序(.Framework)

链接:https://pan.baidu.com/s/1CVjGvL8sTwwKTRBVb0hKnQ
提取码:lqk6

然后再自己的本地系统windows 10环境中,
1,将控制面板-程序-程序与功能-启用或关闭windows功能-internet information services下
web管理工具将IIS 6管理兼容性以及万维网服务下的应用程序开发功能都选上,然后点击确认。
2,将发布包解压到C盘自定义的目录下,
3,在计算机管理-服务和应用程序-Internet Information Services(IIS)管理器中添加网站

部署本身比较简单,这里不过多描述,希望有图文的话可以参考这篇博文的描述的部署内容
https://blog.csdn.net/qq_41481924/article/details/125841256

如果还不能部署成功,可以在百度上搜索关键词“IIS部署webservice”寻找解决方案。

二,调用无参数的webservice接口

我的电脑是192.168.123.216的IP,下列在我的部署的环境上调用helloworld接口,这个接口的作用就是输出“hello world”

以下是 http post 请求和响应示例。所显示的占位符需替换为实际值。

post /webservice1.asmx/helloworld http/1.1
host: 192.168.123.216
content-type: application/x-www-form-urlencoded
content-length: length

http/1.1 200 ok
content-type: text/xml; charset=utf-8
content-length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://192.168.122.129/">string</string>

--函数实现

create or replace function get_http_response (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 => 'post' );
      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;

测试及其结果

select get_http_response( 'http://192.168.123.216:8001/WebService1.asmx/HelloWorld') from dual;

--结果

test=# select get_http_response( 'http://192.168.123.216:8001/WebService1.asmx/HelloWorld') from dual;
test-# /
                      get_http_response
--------------------------------------------------------------
 <string xmlns="http://192.168.122.129/">Hello World</string>
(1 row)

test=#

验证一下:在浏览器中输入

http://192.168.123.216:8001/WebService1.asmx?op=HelloWorld

点击“调用”后的结果一样:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
Hello World

以上验证ok

三,调用有参数的webservice接口

HTTP POST接口调用说明:

以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebService1.asmx/Power HTTP/1.1
Host: 192.168.123.216
Content-Type: application/x-www-form-urlencoded
Content-Length: length

num=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://192.168.122.129/">int</int>

编写函数:

create or replace function get_http_response (v_url  varchar2,v_body varchar2)
return varchar2
as
	req utl_http.req;
	resp utl_http.resp;
	v_line varchar2 ( 32000 );
	v_text varchar2 ( 32000 ) ;
begin
  v_text := '';
  req := utl_http.begin_request ( url => v_url, method => 'post' );
  utl_http.set_body_charset('utf-8');	  
  utl_http.set_header(req, 'content-type', 'application/x-www-form-urlencoded');
  utl_http.set_header(req, 'content-length', length(v_body));

  utl_http.write_text(req, v_body);

  resp := utl_http.get_response ( req );
  
  --raise notice '------->%',resp;
  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 );
  return v_text;
exception
	when utl_http.end_of_body then
		utl_http.end_response ( resp );
		return v_text;
	when others then
		utl_http.end_response(resp);
		utl_http.end_request(req);
		return v_text;
end;

代用代码

DECLARE
   v_rel varchar2(32000);
BEGIN
   v_rel := get_http_response( 'http://192.168.123.216:8001/WebService1.asmx/Power','num=15') ;
 -- dbms_output.put_line( '-----10------' || v_rel);
   raise notice '----->1';
   raise notice '----->%',v_rel;
   raise notice '----->2';
end;
/

测试结果

test=# DECLARE
test-#    v_rel varchar2(32000);
test-# BEGIN
test-#    v_rel := get_http_response( 'http://192.168.123.216:8001/WebService1.asmx/power','num=15') ;
test-#  -- dbms_output.put_line( '-----10------' || v_rel);
test-#    raise notice '----->1';
test-#    raise notice '----->%',v_rel;
test-#    raise notice '----->2';
test-# end;
test-# /
NOTICE:  ----->1
NOTICE:  ----->System.ArgumentException: 无效的方法名“power”,方法名区分大小写。找到了名称相同但大小写不同的方法                                                 在 System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest requ                                              est, HttpResponse response, Boolean& abortProcessing)
NOTICE:  ----->2
ANONYMOUS BLOCK
test=#
test=#
test=#
test=# DECLARE
test-#    v_rel varchar2(32000);
test-# BEGIN
test-#    v_rel := get_http_response( 'http://192.168.123.216:8001/WebService1.asmx/Power','num=15') ;
test-#  -- dbms_output.put_line( '-----10------' || v_rel);
test-#    raise notice '----->1';
test-#    raise notice '----->%',v_rel;
test-#    raise notice '----->2';
test-# end;
test-# /
NOTICE:  ----->1
<int xmlns="http://192.168.122.129/">225</int>tf-8"?>
NOTICE:  ----->2
ANONYMOUS BLOCK
test=#

验证一下,用同样的方法,在浏览器中输入
http://192.168.123.216:8001/WebService1.asmx?op=Power
输入数值15

调用得到的结果:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<int xmlns="http://192.168.122.129/">225</int>

以上验证符合预期

四,小结

本文介绍了基于HTTP POST协议的调用方式,展示了有参数和无参数websevice接口的调用。
实际上还可有GET协议,还可以有基于构造SOAP协议的调用方式,后续会继续介绍。

posted @ 2024-04-01 15:30  KINGBASE研究院  阅读(61)  评论(0编辑  收藏  举报