从PL / SQL调用Web服务失败

时间:2014-02-26 13:48:55

标签: web-services plsql

我只想从PL / SQL调用Web服务,因此我创建了一个Web服务:

  http://localhost:64955/Service1.asmx?op=add

首先,网络方法非常简单,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Calculator
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int add(int firstNum, int secondNum)
        {
            return firstNum + secondNum;
        }
    }
}

我想在Web服务中调用第二个方法(add),所以我写这样的PL / SQL代码:

declare
   l_param_list  varchar2(512);
   l_http_request UTL_HTTP.req;
   l_http_response UTL_HTTP.resp;
   l_response_text varchar2(32000);
begin
-- service's input parameters
   l_param_list := 'firstNum=1'||'&'||'secondNum=2';
--http://localhost:64955/Service1.asmx?op=add
--16.158.161.7
-- prepareint Request...
   l_http_request := UTL_HTTP.begin_request ('http://localhost:64955/Service1.asmx?op=add'
                                            ,'POST'
                                            ,'HTTP/1.1');

--...set header's attributes
UTL_HTTP.set_header(l_http_request,'Content-Type', 'application/x-www-form-urlencoded');
UTL_HTTP.set_header(l_http_request,'Content-Length', length(l_param_list));

--...set input parameters
UTL_HTTP.write_text(l_http_request, l_param_list);

-- get response and obtain received value
l_http_response := UTL_HTTP.get_response(l_http_request);

UTL_HTTP.read_text(l_http_response, l_response_text);

dbms_output.put_line(l_response_text);
dbms_output.put_line('test1');
--finalizing
UTL_HTTP.end_response(l_http_response);

exception
   when UTL_HTTP.end_of_body then
        UTL_HTTP.end_response(l_http_response);
        dbms_output.put_line('test2');

end;

但是当我运行这个PL / SQL代码段时,我遇到了一些例外:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Xml.XmlException: Root element is missing.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res)
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlTextReader.Read()
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
   at System.Xml.XmlReader.MoveToContent()
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
   at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)
   --- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
test1

我是PL / SQL的初学者,所以有人能告诉我我的代码有什么问题吗? 顺便说一句,Web服务可以通过Windows窗体应用程序正常调用。

1 个答案:

答案 0 :(得分:1)

问题是您的Web服务是SOAP,但您没有发送SOAP请求。 要执行此操作,请通过浏览此URL http://localhost:64955/Service1.asmx?wsdl来检查Web服务的wsdl,从那里您将了解如何创建SOAP信封以调用您的web方法。 你会做类似的事情:

soap_request :=
  '<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> 
  <SOAP-ENV:Body>
  <m:add xmlns:m="Some-URI">
  <firstNum>1</firstNum>
  <secondNum>2</secondNum>
  </m:add>
  </SOAP-ENV:Body>  
  </SOAP-ENV:Envelope>';

  l_http_request := UTL_HTTP.begin_request ('http://localhost:64955/Service1.asmx?op=add'
                                        ,'POST'
                                        ,'HTTP/1.1');

  --...set header's attributes
  UTL_HTTP.set_header(l_http_request,'Content-Type', 'application/xml');
  UTL_HTTP.set_header(l_http_request,'Content-Length', length(soap_request));

  --...set input parameters
  UTL_HTTP.write_text(l_http_request, soap_request);
  -- get response and obtain received value
  l_http_response := UTL_HTTP.get_response(l_http_request);
  UTL_HTTP.read_text(l_http_response, l_response_text);
  dbms_output.put_line(l_response_text);
  dbms_output.put_line('test1');
  --finalizing 
  UTL_HTTP.end_response(l_http_response);