首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义SoapHeader php to C#

自定义SoapHeader php to C#
EN

Stack Overflow用户
提问于 2013-01-16 22:42:52
回答 1查看 151关注 0票数 1

如何在C#中使用ServiceReference或WebReference执行此操作

代码语言:javascript
复制
   class SoapAuthentificationHeader {
        /** @var string sPassword */
        public $Password;
        /** @var string sLogin */
        public $sLogin;
        /** @var string anotherCustomParam*/
        public $sVod;

        public function __construct($sLogin, $sPassword, $sVod) {
            $this->sPassword=$sPassword;
            $this->sLogin=$sLogin;
            $this->sVod=$sVod;
        }
    }

    $oSoap=new SoapClient('http://.[ my path ]...wsdl', array(
        'trace' => 1,
        'encoding' => 'UTF-8'
    ));

    try {
        $oSoap->__setSoapHeaders(array(new SoapHeader('urn:vod_soap', 'AuthenticationHeader', new SoapAuthentificationHeader('login', 'password', 'anotherCustomParam'))));     
$iCountVideo = $oSoap->countVideo();
    } catch (Exception $oException) {
        var_dump($oException);
    }

我尝试在我的调用之间实例化一个共享CookieContainer。但它不起作用:目前我有:

代码语言:javascript
复制
vod_soapService s = new vod_soapService();
            s.CookieContainer = new CookieContainer();

            s.ConnectionGroupName = "test";
             // this webmethod works it return me true 
            s.AuthenticationHeader("foo", "bar", "test");

            string test = s.countVideo();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-23 19:07:54

在发送xml之前,必须手动插入soap标头。要实现这一点,请阅读以下链接

http://forums.asp.net/t/1137408.aspx

您还必须调整给定的解决方案以适合您的身份验证头

您的"AfterSerialize“实现应该如下所示:

代码语言:javascript
复制
case SoapMessageStage.AfterSerialize:
{
  // Get the SOAP body as a string, so we can manipulate...
  String soapBodyString = getXMLFromCache();

  String BodyString = "<soap:Body";
  int posStartBody = soapBodyString.IndexOf(BodyString);

  // Create the SOAP header Message 
  String soapEnvHeaderString = "<soap:Header><SoapAuthentificationHeader><sLogin>";
  String soapEnvHeaderString2 = "</sLogin><sPassword>";
  String soapEnvHeaderString3 = "</sPassword><sVod>";
  String soapEnvHeaderString4 = "</sVod></SoapAuthentificationHeader></soap:Header>";
  Stream appOutputStream = new MemoryStream();
  StreamWriter soapMessageWriter = new StreamWriter(appOutputStream);
  soapMessageWriter.Write(soapBodyString.Substring(0,posStartBody));
  soapMessageWriter.Write(soapEnvHeaderString);
  soapMessageWriter.Write("your login ");
  soapMessageWriter.Write(soapEnvHeaderString2);
  soapMessageWriter.Write("your password");
  soapMessageWriter.Write(soapEnvHeaderString3);
  soapMessageWriter.Write("your vod");
  soapMessageWriter.Write(soapEnvHeaderString4);
  soapMessageWriter.Write(soapBodyString.Substring(posStartBody));
  // write it all out.
  soapMessageWriter.Flush();
  appOutputStream.Flush();
  appOutputStream.Position = 0;
  StreamReader reader = new StreamReader(appOutputStream);
  StreamWriter writer = new StreamWriter(this.outputStream);
  writer.Write(reader.ReadToEnd());
  writer.Flush();
  appOutputStream.Close();
  this.outgoing = false;
  this.incoming = true;
  break;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14360919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档