不明白“MustUnderstand”标题

时间:2018-02-12 16:14:38

标签: c# web-services soap

我正在使用供应商的SOAP API。我跟着this example但是在尝试执行客户端命令时遇到了一个奇怪的错误。我对SOAP很陌生,但据我所知,它听起来像是认证不对的。目前,我的代码是:

    private bool EmailExists(string email)
    {
        try
        {
            WSHttpBinding myBinding = new WSHttpBinding();
            myBinding.Security.Mode = SecurityMode.Transport;

            myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

            EndpointAddress ea = new EndpointAddress("https://api.subscribermail.com/ws/latest/smrecipient?wsdl");

            var smrecipientClient = new smrecipient.smrecipientPortTypeClient(myBinding, ea);
            smrecipientClient.ClientCredentials.UserName.UserName = "[obfuscated]";
            smrecipientClient.ClientCredentials.UserName.Password = "[obfuscated]";

            smrecipientClient.Open();

            var data = smrecipientClient.queryRecipientByEmail(email);

            smrecipientClient.Close();

            if (data != null && !string.IsNullOrEmpty(data.id)) return true;
        }
        catch (Exception ex)
        {
            return false;
        }

        return false;
    }

当我跳过var data = smrecipientClient.queryRecipientByEmail(email);时,我在catch块中看到以下错误:

{"Did not understand \"MustUnderstand\" header(s):{http://www.w3.org/2005/08/addressing}Action, {http://www.w3.org/2005/08/addressing}To"}

没有内部异常,但堆栈跟踪是:

Server stack trace: 
   at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at [myApp].Web.SMRecipient.smrecipientPortType.queryRecipientByEmail(String emailAddress)
   at [myApp].Web.SMRecipient.smrecipientPortTypeClient.queryRecipientByEmail(String emailAddress) in C:\Dev\[myApp]\my-website\Web\Connected Services\SMRecipient\Reference.cs:line 1555
   at [myApp].Web.Controllers.SubscriptionsController.EmailExists(String email) in C:\Dev\[myApp]\my-website\Web\Controllers\SubscriptionsController.cs:line 128

我确实看到this SO thread但是不理解解决方案(如何在我的示例中通过代码设置WSS密码类型)。另外,我的一位同事说我需要完全不同地进行身份验证并更新Web服务的Reference.cs,以便主类继承自Microsoft.Web.Services3.WebServicesClientProtocol,但问题是,Web服务的Reference.cs没有有一个主要的班级。

1 个答案:

答案 0 :(得分:1)

WsHttpBinding用于更高级的WS相关可靠消息传递。在你的场景中,简单的BasicHttpBinding也可以正常工作。

你能使用basicHttpBinding吗?:

    <basicHttpBinding> 
     <binding name="https"> 
      <security mode="Transport"> 
       <message clientCredentialType="Basic"/> 
      </security>
     </binding> 
    </basicHttpBinding>
相关问题