2012-06-16 108 views
0

我被給了這個我需要調用的web服務的結構。 ws的名字是Set_Details我把它弄錯了,所以不能導致錯誤。 我無法弄清楚如何解決這個問題。 當我叫它我得到錯誤:未能調用web服務

System.InvalidOperationException:Set_Details Web服務方法名稱無效。在System.Web.Services.Protocols.ServerProtocol.SetContext(類型類型,HttpContext上下文,HttpRequest請求,HttpResponse響應)System.Web.Services.Protocols.ServerProtocolFactory上System.Web.Services.Protocols.HttpServerProtocol.Initialize()。創建(類型類型,HttpContext的背景下,HttpRequest對象的要求,HttpResponse對象響應,布爾& abortProcessing)

*我改變了真實的URL路徑,假地址,請忽略網址

<?xml version="1.0" encoding="utf-8"?> 
<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> 
    <Set_Details xmlns="http://ws.blobobobo.uk/WebService/"> 
     <Details> 
     <Number>int</Number> 
     <Category>string</Category> 
     </Details> 
     <LoginID>string</LoginID> 
     <LoginPassword>string</LoginPassword> 
    </Set_Details> 
    </soap:Body> 
</soap:Envelope> 

這是我的代碼,我致電課程網上服務

dim ws 
     set ws = new webservice 
     ws.url = mainip 
     ws.method = "Set_OrderDetails" 

     ws.parameters.Add "Number", "6166"" 
     ws.parameters.Add "Category", "ffff" 

     ws.parameters.Add "LoginID", "ex10" 
     ws.parameters.Add "LoginPassword", "ex20" 


    ws.execute 


    response.write ws.response 
set ws = nothing 

這是我的課誰代表了Web服務

class WebService 
    public Url 
    public Method 
    public Response 
    public Parameters 

    public function execute() 
     dim xmlhttp 

     Set xmlhttp = CreateObject("Microsoft.XMLHTTP") 
     xmlhttp.open "POST", Url & "/" & Method, false 
     xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
     xmlhttp.send Parameters.toString 
     response = xmlhttp.responseText 
     set xmlhttp = nothing 
    end function 

    Private Sub Class_Initialize() 
     Set Parameters = new wsParameters 
    End Sub 

    Private Sub Class_Terminate() 
     Set Parameters = Nothing 
    End Sub 

end class 

class wsParameters 
    public mCol 

    public function toString() 
     dim nItem 
     dim buffer 

     buffer = "" 
     for nItem = 1 to Count 
      buffer = buffer & Item(nItem).toString & "&" 
     next 
     if right(buffer,1)="&" then 
      buffer = left(buffer,len(buffer)-1) 
     end if 
     toString = buffer 
    end function 


    public sub Clear 
     set mcol = nothing 
     Set mCol = CreateObject("Scripting.Dictionary") 
    end sub 

    public sub Add(pKey,pValue) 
     dim newParameter 

     set newParameter = new wsParameter 
     newParameter.Key = pKey 
     newParameter.Value = pValue 
     mCol.Add mCol.count+1, newParameter 

     set newParameter = nothing 
    end sub 

    public function Item(nKey) 
     set Item=mCol.Item(nKey) 
    end function 

    public function ExistsXKey(pKey) 
     dim nItem 

     for nItem = 1 to mcol.count 
      if mCol.Item(nItem).key = pKey then 
       ExistsXKeyword = true 
       exit for 
      end if 
     next 
    end function 

    public sub Remove(nKey) 
     mCol.Remove(nKey) 
    end sub 

    public function Count() 
     Count=mCol.count 
    end function 

    Private Sub Class_Initialize() 
     Set mCol = CreateObject("Scripting.Dictionary") 
    End Sub 

    Private Sub Class_Terminate() 
     Set mCol = Nothing 
    End Sub 
end class 

class wsParameter 
    public Key 
    public Value 

    public function toString() 
     toString = Key & "=" & Value 
    end function 
end class 


Dim ip 
mainip="http://ws.bloblobo.uk/WebServices/ws.asmx" 

回答

0

當調用這樣做,你應該構建一個原始HTTP POST和Web服務描述的那樣發送整個請求你的SOAP的Web服務方法。即你需要發送:

<?xml version="1.0" encoding="utf-8"?> 
<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> 
    <Set_Details xmlns="http://ws.blobobobo.uk/WebService/"> 
     <Details> 
     <Number>6166</Number> 
     <Category>ffff</Category> 
     </Details> 
     <LoginID>ex10</LoginID> 
     <LoginPassword>ex20</LoginPassword> 
    </Set_Details> 
    </soap:Body> 
</soap:Envelope> 

在你剛剛張貼的時刻:

Number=6166&Category=ffff&LoginID=ex10&LoginPassword=ex20 

的web服務,管道不知道該怎麼做這一點。

您的Web服務方法也接受複雜類型Details,因此您必須將請求作爲HTTP POST發送,並使用完整胖請求(信封,正文等)發送。

您還需要設置SOAPAction HTTP頭中的要求,以及如:

SOAPAction: "http://ws.blobobobo.uk/WebService/Set_Details" 

更新時間:

這裏是一個完全樣例:

<% 
result = Set_Details(6166, "ffff", "ex10", "ex20") 
Response.Write result 

Function Set_Details(number, category, loginID, loginPassword) 

    Dim request, doc, xmlHttp, url 
    url = "http://ws.bloblobo.uk/WebServices/ws.asmx" 

    request = "<?xml version='1.0' encoding='utf-8'?>" & _ 
    "<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>" & _ 
    " <Set_Details xmlns='http://ws.blobobobo.uk/WebService/'>" & _ 
    "  <Details>" & _ 
    "  <Number>" & number & "</Number>" & _ 
    "  <Category>" & category & "</Category>" & _ 
    "  </Details>" & _ 
    "  <LoginID>" & loginID & "</LoginID>" & _ 
    "  <LoginPassword>" & loginPassword & "</LoginPassword>" & _ 
    " </Set_Details>" & _ 
    " </soap:Body>" & _ 
    "</soap:Envelope>" 

    Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP") 
    xmlHttp.open "POST", url, False 
    xmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8" 
    xmlHttp.setRequestHeader "SOAPAction", _ 
      """http://ws.blobobobo.uk/WebService/Set_Details""" 

    xmlHttp.send request 

    Set response = xmlHttp.responseXML 

    '' The presumption here is that you get some kind of result back. 
    '' The example here assumes that the `Set_Details` method returns a simple type 
    '' such as a string, number or boolean 
    result = response.documentElement.selectSingleNode("//Set_DetailsResult").text 

    Set_Details = result 

End Function 

%> 

最後從不使用Microsoft.XMLHTTP發出HTTP請求,它不適用於ASP或服務器端應用程序,請使用0123改爲。

+0

好吧我現在明白,但不知道如何做到這一點與我目前的代碼結構 - 是否可能或我需要改變所有類? – Bobo2012

+0

@ Bobo2012 - 我提供了一個完整的例子。 – Kev

+0

不要使用字符串連接來構建XML。使用XML DOM來創建XML。如果傳入數據包含諸如'<'和'&'的字符,這將確保正確的編碼。它還將確保XML的格式良好。 – AnthonyWJones