【问题标题】:How to pass single tag header in php soap client for wcf?如何在 php soap 客户端中为 wcf 传递单个标签标头?
【发布时间】:2015-08-25 10:35:41
【问题描述】:

我正在尝试从 PHP 使用 WCF 服务。我需要在 PHP 的肥皂客户端中传递一个标头字段才能使用它。肥皂标题将是这样的:

<header>
<LisenseKey>Lisense key goes here</LisenseKey>
</header>

LisenseKey 元素的命名空间是“http://mylinsensekeynamespace”。

我想在WCF中消费的方法如下:

public string function GetMessage(string name)
{
    return "Hello , "+name;
}

在将服务配置为验证标头之前,我从 PHP 中使用该服务,如下所示,它运行良好:

try{
    $client = new SoapClient("http://localhost:8181/?wsdl");
    $param = new stdClass();
    $param->name = "My Name";
    $webService = $client->GetMessage($param);
    print_r($webService);
}
catch(Exception $e)
{
    echo $e->getMessage();  
}

当服务配置为验证标头中的许可证密钥后,我试图像这样使用它,但它还没有工作:

try{
    $client = new SoapClient("http://localhost:8181/?wsdl");

    $actionHeader = new SoapHeader("http://mycustomheader",'LisenseKey',"lisense key",true);
    $client->__setSoapHeaders($actionHeader);
    $param = new stdClass();
    $param->name = "My Name";
    $webService = $client->GetMessage($param);
    print_r($webService);
}
catch(Exception $e)
{
    echo $e->getMessage();  
}

我已经尝试了许多与在线文章不同的方式。我该如何消费它? WCF 服务使用的是 BasicHttpBinding,SOAP 版本应该是 1.1。如何传递头部信息来消费服务?

以下是 .NET WCF 服务代码,用于验证每个请求的 LicenseKey soap 标头。

    public class MyServiceMessageInspector : System.ServiceModel.Dispatcher.IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel,
            System.ServiceModel.InstanceContext instanceContext)
        {
            if (request.Headers.FindHeader("LisenseKey", "") == -1)
            {
                throw new FaultException("Lisense Key Was Not Provided");
            }

            var lisenseKey = request.Headers.GetHeader<string>("LisenseKey", "http://mycustomheader.com");
            if (string.IsNullOrEmpty(lisenseKey))
            {
                throw new FaultException("Lisnse key should not be empty");
            }

            if (lisenseKey != "12345x")
            {
                throw new FaultException("Lisense key is not valid");
            }

            return instanceContext;
        }

        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {

        }
    }

    public class MyServiceMessageInspectorBehaviour : Attribute, System.ServiceModel.Description.IServiceBehavior
    {
        public void AddBindingParameters(System.ServiceModel.Description.ServiceDescription serviceDescription,
            System.ServiceModel.ServiceHostBase serviceHostBase,
            System.Collections.ObjectModel.Collection<System.ServiceModel.Description.ServiceEndpoint> endpoints,
            System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {

        }

        public void ApplyDispatchBehavior(System.ServiceModel.Description.ServiceDescription serviceDescription,
            System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
            {
                foreach (var endpointDispatcher in channelDispatcher.Endpoints)
                {
                    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyServiceMessageInspector());
                }
            }
        }

        public void Validate(System.ServiceModel.Description.ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {

        }
    }

【问题讨论】:

  • 你遇到了什么错误?

标签: php wcf soap


【解决方案1】:

尝试像这样调用 WCF 服务:$client-&gt;__soapCall("GetMessage", $param, NULL, $header);

【讨论】:

  • 请问 $header 变量的结构是什么?
  • 如果标题不是强制性的,请尝试将 mustUnderstand 参数设置为 false : new SoapHeader("http://mycustomheader",'LisenseKey',"&lt;license-key&gt;",false);。还要确保您传递了正确的许可证密钥。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 2011-05-09
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多