使用 ServerXMLHTTP 连接到 Web 服务

Using ServerXMLHTTP to connect to web service

我有一个经典的 asp 文件需要连接到网络服务。 我对如何在 SOAPAction 中为 Web 服务和命名空间定义 URL 感到困惑。 当我 运行 我的代码并为我在 Web 服务中调用的方法的 return 值编写 Response.Write 时,它 returns wsdl 或 web服务页面

此代码显示网络服务 html 就像您正在输入网络服务 .svc url:

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。 如何调用网络服务中的方法?

更新 我将代码更改为以下内容:

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 - 不支持的媒体 我看到 post 有这个错误,他们通过更改 Content-Type 来纠正它。当我尝试这个时: httpRequest.setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"

我得到状态值 400 - 错误请求。

终于想通了。下面是我的代码,以防一些可怜的仆从需要这样做......也许它会减轻痛苦并缩短达到 运行 的时间。 我将 WCF Web 服务(文件扩展名 .svc)连接到经典的 .asp 文件。 我必须向 Web 服务添加一个 basicHttpBinding。它也可以是安全的 (HTTPS)。 这是添加到服务配置的绑定。文件: (注意它的名称。由 address 属性定义。

<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPaging" contract="PagingService.IPaging">
</endpoint>

将此添加到配置文件的 bindings 部分:

<basicHttpBinding> 
    <binding name="BasicHttpBinding_IPaging"></binding>
</basicHttpBinding>

您需要重新编译项目,以便 WSDL 在其定义中具有此绑定。 现在是有趣的部分....经典 asp :( 用 VBScript 编写。 SOAP信封最让我头疼的是:

'Namespaces
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD, NS_SOAP_ACTION
'NS is the name of YOUR namespace.  If you did not define it
'in the service interface it is probably the same as this
NS = "http://tempuri.org/"
'It is for SOAP 1.1 - my version of .asp could only use SOAP 1.1
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
'Next 3 definitions are standard - just copy
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"
'This should also be in your WSDL.  Look up the method you
'want to call; there was an attribute in mine that read 'soap_action'
NS_SOAP_ACTION = "http://tempuri.org/IFileName/<YourMethodName>"

'URL to the WCF service Using basicHttpBinding identified to the name
'you defined in the config file in 'address' attribute
 URL = "https://<serverName>:<port>/ServiceFolder/Service.Paging.svc/basic"

 'This was the hard part for me.  Defining the damn soap message
 'XML DOM objects.
 Dim Envelope, Body, Operation
 Dim ParamUserID, ParamUserName, 

 'Creates an XML DOM object.
 Set objXmlDoc = CreateObject("MSXML2.DOMDocument.6.0") 
 objXmlDoc.async = false

 'Creates the main elements.
 Set Envelope = objXmlDoc.createNode(1, "soap:Envelope", NS_SOAP)
 Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
 Envelope.setAttribute "xmlns:xsi", NS_XSI
 Envelope.setAttribute "xmlns:xsd", NS_XSD
 objXmlDoc.appendChild Envelope

 Set Body = objXmlDoc.createNode(1, "Body", NS_SOAP)
 Envelope.appendChild Body 

 'Creates an element for the SendPageForGalvanonSystem function.
 Set Operation = objXmlDoc.createNode(1, "<MethodName>", NS)
 Body.appendChild Operation 

 'Add all the parameters to the DOM
 Set ParamUserID = objXmlDoc.createNode(1, "strUserID", NS)
 ParamUserID.text = strUserID
 Operation.appendChild ParamUserID

 Set ParamUserName = objXmlDoc.createNode(1, "strUserName", NS)
 ParamUserName.text = strUserName
 Operation.appendChild ParamUserName

 Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
 httpRequest.Open "POST", URL, False         
 httpRequest.setRequestHeader "Content-Type", "text/xml"     
 httpRequest.setRequestHeader "SOAPAction", NS_SOAP_ACTION
 httpRequest.send objXmlDoc.xml

 'Releases the objects.
 Set ParamUserID = Nothing
 Set ParamUserName = Nothing
 Set Operation = Nothing
 Set Body = Nothing
 Set Envelope = Nothing