自定义SoapHeader php到c#

本文关键字:php SoapHeader 自定义 | 更新日期: 2023-09-27 17:53:49

如何在c#中使用servicerreference或webrereference

   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。但它没有工作:目前我有:

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();

自定义SoapHeader php到c#

您必须在发送xml之前手动插入soap头。要做到这一点,请阅读下面的链接
http://forums.asp.net/t/1137408.aspx

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

你的"AfterSerialize"实现应该是这样的:

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;
}