C#/PHP调用有SoapHeader的WebService

Posted 超软毛毛虫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#/PHP调用有SoapHeader的WebService相关的知识,希望对你有一定的参考价值。

日前调用第三方WebService接口时遇到了SoapHeader验证的问题,记录一下解决方法。

接口需要的格式:

<soapenv:Header>
    <ReqSOAPHeader xmlns="http://xxx.com">
        <appKey>key</appKey>
        <authKey>pwd</authKey>
    </ReqSOAPHeader>
<soapenv:Header>

C#:

    //创建header类,注意属性名
    [DataContract(Namespace = "http://xxx.com")]
    public class ReqSOAPHeader
    {
        [DataMember]
        public string appKey { get; set; }
        [DataMember]
        public string authKey { get; set; }
    }

    //client 为根据wsdl生成的代理类
    //创建并加入请求
    AddressHeader soapheader = AddressHeader.CreateAddressHeader("ReqSOAPHeader",   "http://xxx.com", new ReqSOAPHeader { appKey = "key", authKey = "pwd" });
    EndpointAddressBuilder eab = new EndpointAddressBuilder(client.Endpoint.Address);
    eab.Headers.Add(soapheader);
    client.Endpoint.Address = eab.ToEndpointAddress();

 

php:

//创建Header类
//注意属性名
    class ReqSOAPHeader {  
        public $appKey;  
        public $authKey;  
        
        public function __construct($app,$auth) {  
            $this->appKey=$app;  
            $this->authKey=$auth;  
        } 
    }

//创建soapclient并加入头
    $simple = new SoapClient(null, array(‘location‘=>$location,‘uri‘=>‘xxx.com‘,‘encoding‘=>‘UTF-8‘));
    $auth = new ReqSOAPHeader("key", "pwd");
    $header = new SoapHeader(‘http://xxx.com‘, ‘ReqSOAPHeader‘, $auth);
    $simple->__setSoapHeaders($header);

 

以上是关于C#/PHP调用有SoapHeader的WebService的主要内容,如果未能解决你的问题,请参考以下文章

请教一个python调用webservice时进行soapheader认证的问题

WCF安全性认证:SoapHeader使用HTTP Request调用

java调用webservice怎么添加 SoapHeader 做验证

Google Adwords API 异常:调用公共抽象 SimpleMutateJob 时有多个 SoapHeader

java axis调用带有soap头(soapheader)的.net webservice

C#在做WSDL生成客户端代码,无法添加soapheader