douhuigan8063 2014-09-25 14:16
浏览 134
已采纳

C#中的SoapHeader身份验证(cleverelements)

I am currently trying to consume a SOAP WebService via C#.

The WebServices uses some header authentication I don't get working in C# There is some sample PHP Code, but since it's my first SOAP Client I don't really get the C# equivalent.

I hope somebody can tell me how to do the following in C#:

$client = new SoapClient("http://api.sendcockpit.com/server.php?wsdl"); 
class SOAPAuth{     
    public $userid;     
    public $apikey;
    public $version;
    public $mode;
    public function __construct($userid, $apikey, $version, $mode = 'test') {
        $this--->userid = $userid;
        $this->apikey = $apikey;
        $this->version = $version;
        $this->mode = $mode;
    }
}

$auth = new SOAPAuth('User ID','API Key','1.0','test');

$header = new SOAPHeader('sendcockpit', 'validate', $auth);

$client->__setSoapHeaders($header);

try{
    //get all subscriber lists from account
    $response=$client->apiGetList();
}
catch (SoapFault $exception) {
    echo ($exception->getMessage());
}
  • 写回答

2条回答 默认 最新

  • duanhuilao0787 2014-11-06 11:12
    关注

    Sorry for the late reply.

    The solution I chose was to use a service reference and add the headers to the web.config like this:

    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="APIServiceBinding" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://api.sendcockpit.com/server.php" binding="basicHttpBinding"
      bindingConfiguration="APIServiceBinding" contract="Newsletter.APIServicePortType"
      name="OrdersPort" >
            <headers>
              <validate>
                <apikey>[yourAPIKey]</apikey>
                <userid>[yourUserId]</userid>
                <version>1.0</version>
                <mode>live</mode>
              </validate>
            </headers>
          </endpoint>
        </client>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    </system.serviceModel>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题