我有一個經典的asp文件需要連接到一個web服務。 我很困惑如何定義Web服務的URL和SOAPAction中的名稱空間。 當我運行代碼併爲我在Web服務中調用的方法的返回值編寫Response.Write
時,它將返回wsdl或服務的網頁使用ServerXMLHTTP連接到Web服務
此代碼顯示web服務html爲如果你正在輸入Web服務的.svc網址:
Dim strSoapReq
strSoapReq = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
strSoapReq = strSoapReq & "<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">"
strSoapReq = strSoapReq & "<s:Body>"
strSoapReq = strSoapReq & "<TestMethod xmlns=""http:<serverName:<port>/PagingService/PagingService"">"
strSoapReq = strSoapReq & "</TestMethod>"
strSoapReq = strSoapReq & "</s:Body>"
strSoapReq = strSoapReq & "</s:Envelope>"
'Create server-side component to make requests
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
URL = "http:<serverName:<port>/PagingService/PagingService.Paging.svc"
httpRequest.Open "GET", URL, False
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
httpRequest.setRequestHeader "SOAPAction", URL & "/TestMethod"
httpRequest.Send(strSoapReq)
Dim strResult
strResult = httpRequest.responseText
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)
如果我添加了?wsdl
到服務URL的末尾,它顯示了WSDL。 如何在Web服務中調用方法?
UPDATE 我改變了我的代碼如下:
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD
NS = "http://<server>/PagingService/"
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
URL = "http://<server>/PagingService/PagingService.Paging.svc?WSDL"
httpRequest.Open "POST", URL, False
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
httpRequest.setRequestHeader "SOAPAction", "http://<server>/PagingService/TestMethod"
' XML DOM objects.
Dim DOM, Envelope, Body, Operation, Param
' Creates an XML DOM object.
Set DOM = CreateObject("MSXML2.DOMDocument.6.0")
' Creates the main elements.
Set Envelope = DOM.createNode(1, "soap:Envelope", NS_SOAP)
Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
Envelope.setAttribute "xmlns:xsi", NS_XSI
Envelope.setAttribute "xmlns:xsd", NS_XSD
DOM.appendChild Envelope
Set Body = DOM.createElement("soap:Body")
Envelope.appendChild Body
' Creates an element for the TestMethod function.
Set Operation = DOM.createNode(1, "TestMethod", NS)
Body.appendChild Operation
' Releases the objects.
Set Operation = Nothing
Set Body = Nothing
Set Envelope = Nothing
httpRequest.Send(DOM.xml)
Dim strResult
strResult = httpRequest.status
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)
的http.Status這段代碼的結果是:415 - 不支持的媒體 我看到這個錯誤後,他們被糾正之後,改變了Content-Type
。當我試過這個: httpRequest.setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"
我得到了400的狀態值 - 錯誤的請求。
如果您在標頭中忽略charset = utf-8,會發生什麼情況? – Sourcery
所以它應該閱讀:'httpRequest.setRequestHeader「Content-Type」,「text/xml」'? –
所以,我改變了上面的一行,沒有改變。另外,我嘗試在'Open'語句中使用'GET'和'POST'。 'GET'返回描述Web服務的wsdl或網站。 'POST'不會返回任何內容。也沒有錯誤。 –