使用经典 asp 文件中的 basicHttpBinding 调用 Web 服务

calling web service using basicHttpBinding from classic asp file

我有一个 WCF Web 服务,它使用 wsHttpBinding 连接到大多数应用程序。我必须从经典 asp 文件连接到此 Web 服务。根据我阅读和尝试的内容,我需要使用 basicHttpBinding 来执行此操作。它是 SOAP 1.1,要使用 webhttpbinding,我必须通过添加 [WebGet] 来更改 Web 服务接口,这不是一个选项。所以我将 basicHttpBinding 添加到 Web 服务。

我使用控制台应用程序 运行 asp 文件。当它是 运行 时,我得到错误:

The message with Action 'urn:http://tempuri.org/IPaging/TestMethod' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).

我不明白我哪里出错了。我将 URL 设置为调用基本绑定。 SOAP 消息使用的是 1.1,我相信它是正确的。

为什么发送方和接收方不匹配?
客户端没有 app.config 文件来定义连接。它只是 1 asp 个文件。如果我需要,asp 文件如何读取 app.config 文件?

配置文件如下:

<system.serviceModel>
<services>
  <service name="PagingService.Paging" behaviorConfiguration="SimpleServiceBehavior">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IPaging" contract="PagingService.IPaging">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPaging" contract="PagingService.IPaging">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732/Design_Time_Addresses/PagingService/Service1/"/>
      </baseAddresses>
    </host>
  </service>
</services>
<bindings>
  <wsHttpBinding>
    <binding name="WSHttpBinding_IPaging" maxReceivedMessageSize="20000000" maxBufferPoolSize="20000000" sendTimeout="00:25:00">
      <readerQuotas maxDepth="32" maxArrayLength="200000000" maxStringContentLength="200000000"/>
    </binding>
  </wsHttpBinding>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IPaging">
      <security mode="None"></security>
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="SimpleServiceBehavior">      
      <serviceMetadata httpGetEnabled="True"/>      
      <serviceDebug includeExceptionDetailInFaults="False"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

我想从经典 asp 文件的分页服务中调用方法 TestMethod 这是描述测试方法的 WSDL:

<wsdl:operation name="TestMethod">
      <wsdl:input wsaw:Action="http://tempuri.org/IPaging/TestMethod" message="tns:IPaging_TestMethod_InputMessage" /> 
      <wsdl:output wsaw:Action="http://tempuri.org/IPaging/TestMethodResponse" message="tns:IPaging_TestMethod_OutputMessage" /> 
</wsdl:operation>

<wsdl:operation name="TestMethod">
  <soap:operation soapAction="http://tempuri.org/IPaging/TestMethod" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>

这是 basicHttpBinding 在 WSDL 中的定义方式:

<wsdl:port name="BasicHttpBinding_IPaging" binding="tns:BasicHttpBinding_IPaging">
<soap:address location="http://<server>:<port>/Fld1/PagingService.Paging.svc/basic" /> 
</wsdl:port>

这是调用 Web 服务的 asp 代码:

Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD
NS = "urn:http://tempuri.org/"
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"

'Using basicHttpBinding
URL = "http://<server><port>/Fld1/PagingService.Paging.svc/basic"

' XML DOM objects.
Dim DOM, Envelope, Body, Operation, Param 

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

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", "http://schemas.xmlsoap.org/soap/envelope/")
Envelope.appendChild Body 
Set Operation = objXmlDoc.createNode(1, "TestMethod", NS)
Body.appendChild Operation 

Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
httpRequest.Open "POST", URL, False    
httpRequest.setRequestHeader "Content-Type", "text/xml"  
httpRequest.setRequestHeader "SOAPAction", "urn:http://tempuri.org/IPaging/TestMethod"
httpRequest.send objXmlDoc.xml

strStatusText = "Status: " & httpRequest.status & vbCrLf & "Status text: " & httpRequest.statusText
Response.Write(vbCrLf & strStatusText & vbCrLf)  
Response.Write(httpRequest.responseText)

Set Operation = Nothing
Set Body = Nothing
Set Envelope = Nothing

想通了。 从 NS 常量和 SOAPAction 字符串中删除了 urn:,它起作用了。