准备工作
定义测试的webservice及其中的方法
如何发布全新的webservice并测试,可以参考博客c# webservice 接口编写、发布与测试
方法体如下
[webmethod] public string testprocedure(string sinput) { return "执行时间:" + datetime.now.tostring() + sinput; }
oracle语句详情
-- 声明变量和数据类型 declare req utl_http.req; -- http请求句柄 resp utl_http.resp; -- http响应句柄 url varchar2(4000) := 'http://10.xx.xx.xx:8085/webservice.asmx'; -- web service的url地址 soap_env varchar2(4000); -- soap请求包体 buffer varchar2(32767); -- 字符串缓冲区 utf8_data clob; -- 存储utf-8编码的clob数据类型(存储大量文本数据的数据类型) raw_data raw(32767); -- raw数据类型,用于存储二进制数据 raw_buffer raw(32767); -- raw类型的缓冲区 line varchar2(32767); -- 每行读取的数据 idx integer := 1; -- 循环索引变量 begin -- 构造soap请求包体 soap_env := '<soap:envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">' || '<soap:header/>' || -- soap头,这里为空 '<soap:body>' || -- soap主体开始 ' <tem:testprocedure>' || -- 调用web service的方法名 ' <tem:sinput>' || -- 方法参数开始 ' testconnect + 中文test' || -- 参数值,包括英文和中文字符 '</tem:sinput>' || -- 方法参数结束 ' </tem:testprocedure>' || -- 方法调用结束 '</soap:body>' || -- soap主体结束 '</soap:envelope>'; -- soap包体结束 -- 开始发起http post请求,如果此部分存在疑问,请自行寻找前端请求报文相关内容学习。 req := utl_http.begin_request(url, 'post', 'http/1.1'); -- 初始化http请求 utl_http.set_header(req, 'content-type', 'application/soap+xml; charset=utf-8'); -- 设置content-type头部 utl_http.set_header(req, 'content-length', length(soap_env)); -- 设置content-length头部 utl_http.write_text(req, soap_env); -- 写入soap请求包体到http请求 -- 获取http响应 resp := utl_http.get_response(req); -- 获取http响应句柄 dbms_lob.createtemporary(utf8_data, true); -- 创建临时clob变量用于存储响应数据 -- 循环读取http响应体 loop begin utl_http.read_raw(resp, raw_buffer, 32767); -- 读取响应体到raw缓冲区 dbms_lob.writeappend(utf8_data, -- 将raw缓冲区的数据追加到clob中 utl_raw.length(raw_buffer), -- 数据长度 utl_raw.cast_to_varchar2(raw_buffer)); -- 将raw转换为varchar2再写入clob exception when utl_http.end_of_body then exit; -- 如果到达响应体结尾,则退出循环 when others then dbms_output.put_line('在读取响应时发生错误: ' || sqlerrm); -- 其他异常处理 exit; -- 退出循环 end; end loop; utl_http.end_response(resp); -- 结束http响应 -- 循环读取并打印clob中的内容 idx := 1; while idx <= dbms_lob.getlength(utf8_data) loop line := dbms_lob.substr(utf8_data, 255, idx); -- 从clob中读取一行数据 dbms_output.put_line(line); -- 打印读取的一行数据 idx := idx + 255; -- 更新索引 end loop; dbms_lob.freetemporary(utf8_data); -- 释放临时clob空间 exception when utl_http.end_of_body then utl_http.end_response(resp); -- 异常处理:如果到达响应体结尾,确保关闭http响应 when others then dbms_output.put_line('在调用web服务时发生错误: ' || sqlerrm); -- 其他异常处理 if dbms_lob.istemporary(utf8_data) = 1 then 传参的中文会乱码,但是方法内部的中文不会乱码 dbms_lob.freetemporary(utf8_data); -- 释放临时clob空间 end if; end; /
重要参数说明
上述程序已经解决了中文乱码的问题,但是还是不太完美,传参的中文会乱码,但是方法内部的中文不会乱码。
web service的url地址
url varchar2(4000) := 'http://10.xx.xx.xx:8085/webservice.asmx';
构造soap请求包体
soap_env := '<soap:envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">' || '<soap:header/>' || -- soap头,这里为空 '<soap:body>' || -- soap主体开始 ' <tem:testprocedure>' || -- 调用web service的方法名 ' <tem:sinput>' || -- 方法参数开始 ' testconnect + 中文test' || -- 参数值,包括英文和中文字符 '</tem:sinput>' || -- 方法参数结束 ' </tem:testprocedure>' || -- 方法调用结束 '</soap:body>' || -- soap主体结束 '</soap:envelope>'; -- soap包体结束
其中testprocedure
为webservice中定义的测试方法名,sinput
为方法的参数,多个参数,自行添加。标签<tem:sinput>
和</tem:sinput>
中间填写这个参数传的实际值,其余部分无需修改。
构造soap请求包体方法
我使用了soapui这个工具,怎么使用可以参考博客soapui 测试webservice接口可用性
依次如下操作即可:
如果需要把上面的功能变成function或者procedure,请自行搜索相关的方法实现即可。
到此这篇关于oracle通过procedure调用webservice接口的全过程的文章就介绍到这了,更多相关oracle procedure调用webservice内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论