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 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?