2014-03-05 28 views
1

我編寫了一些PLSQL代碼來使用我的本地PC中創建的Web服務,但我總是得到一個不受支持的媒體類型異常,例外代碼爲415。見下面我的代碼:從PL/SQL調用Web服務時得到不受支持的媒體類型

PLSQL:

declare 
    l_param_list varchar2(512); 
    l_http_request UTL_HTTP.req; 
    l_http_response UTL_HTTP.resp; 
    l_response_text varchar2(32000); 
    l_soap_request varchar2(32000); 
begin 
-- service's input parameters 
    l_soap_request := '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <add xmlns="http://tempuri.org/"> 
     <firstNum>3</firstNum> 
     <secondNum>4</secondNum> 
    </add> 
    </soap:Body> 
</soap:Envelope>'; 
--http://localhost:64955/Service1.asmx?op=add 
--16.158.161.7 
dbms_output.put_line('length of l_soap_request :' || length(l_soap_request)); 
dbms_output.put_line('l_soap_request : ' || l_soap_request); 
-- prepareint Request... 
    l_http_request := UTL_HTTP.begin_request ('http://localhost:64955/Service1.asmx' 
              ,'POST' 
              ,UTL_HTTP.HTTP_VERSION_1_1); 

--...set header's attributes 
UTL_HTTP.set_header(l_http_request,'Content-Type', 'text/xml; charset=utf-8'); 
UTL_HTTP.set_header(l_http_request,'Content-Type', length(l_soap_request)); 
UTL_HTTP.set_header(l_http_request,'SOAPAction','http://tempuri.org/add'); 
--...set input parameters 
UTL_HTTP.write_text(l_http_request, l_soap_request); 

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

dbms_output.put_line('Response Received'); 
dbms_output.put_line('________'); 
dbms_output.put_line('Status Code : '||l_http_response.status_code); 
--UTL_HTTP.read_text(l_http_response, l_response_text); 
dbms_output.put_line('Reason Phase : '||l_http_response.reason_phrase); 
--dbms_output.put_line(l_response_text); 
--dbms_output.put_line('test1'); 
utl_http.read_text(l_http_response,l_response_text); 
dbms_output.put_line('Response : '); 
dbms_output.put_line(l_response_text); 
--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; 

Web服務是由.NET在C#創建:

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 string add(string firstNum, string secondNum) 
     { 
      int p1 = Convert.ToInt32(firstNum); 
      int p2 = Convert.ToInt32(secondNum); 
      return (p1 + p2).ToString(); 
     } 
    } 
} 

任何人可以幫助我解決什麼是錯我的代碼或媒體類型,什麼是正確的介質類型或如何知道正確的是?

回答

0

您將要覆蓋內容類型,長度

UTL_HTTP.set_header(l_http_request,'Content-Type', length(l_soap_request)); 

相反,你應該做的:

UTL_HTTP.set_header(l_http_request,'Content-Length', length(l_soap_request)); 
+0

哦,MGD,這是我的錯誤。謝謝你的幫助。 –

相關問題