2014-02-26 96 views
1

失敗我只是想打電話從PL/SQL Web服務,所以我創建了一個Web服務:調用Web服務從PL/SQL

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服務的第二個方法(添加),所以我寫我的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

問題是您的web服務是SOAP,但您沒有發送SOAP請求。 要做到這一點,通過瀏覽這個URL http://localhost:64955/Service1.asmx?wsdl來檢查你的webservice的wsdl,從那裏你將知道如何創建SOAP信封來調用你的webmethod。 你會這樣做:

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);